Join 300,413 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,594 people online right now. Registration is fast and FREE... Join Now!
ok i have searched everywhere and this will be like my 3rd post to different sites requesting some help on this.
i am trying to create an admin side of a website. this side should be able to view images that users have uploaded by username. i have the script to upload the images already. the script creates a file for the user with their username in a primary file that will be on the server to hold all users. i know im supposed to save the location of where the image is and store the location in the database. but how exactly do i go about it? do i need a second php file that has this new script or can i put a few lines of code in the one i have?
Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
Post the code you're having problems with
Post the exact error you're receiving, if you are receiving one
If no error explain what the code is doing versus what you want it to do
Post your question in the body of your post, not the description field
Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
Post the code you're having problems with
Post the exact error you're receiving, if you are receiving one
If no error explain what the code is doing versus what you want it to do
Post your question in the body of your post, not the description field
no im not getting any errors with the upload script. i am unsure how to go about createing the download/display script. i have one that is used to create like an image gallery but that is not exactly what i want. and yes i know im supposed to post my code i have actually posted it in a different thread here and since changed the code i used. if you want i can post here but it works exactly they way i want.
what im not to sure about is. if i just insert the new location in the database for the user's images. how do i request them to be display/downloaded once i have the location in the database? i dont expect you to give me code i just need an idea of how to go about it.
i saw somewhere on here that someone was going to do a tutorial on how to do this but i didnt find the tutorial.
This post has been edited by NeekWorld: 30 Jun, 2009 - 03:00 PM
If your storing the location of the image in the DB than just pull it when needed and wrap it in a img tag ...
ok that makes sense but what if i dont know the total number of images to be displayed? and i have another question. ive been playing with it. it is still uploading the images fine to the file. but the sql statement is not actually inserting the file location or the info i set it to enter into the database. im not getting an error on it. its just not inserting it into the database.
here is the code im using for the insert
CODE
if ($sizeOK && $typeOK){ switch($_FILES['image']['error']) { case 0: // $username would normally come from a session variable $username = $_SESSION['login']; // if the subfolder doesn't exist yet, create it if (!is_dir(UPLOAD_DIR.$username)) { mkdir(UPLOAD_DIR.$username); } // make sure file of same name does not already exist if (!file_exists(UPLOAD_DIR.$username.'/'.$file)) { // get the date and time ini_set('date.timezone', 'Europe/London'); $now = date('Y-m-d-His'); // move the file to the upload folder and rename it $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
mysql_select_db($database, $conn);
//sql query to insert file name into database $sql2 = "SELECT userName FROM users WHERE userName='$username'"; $result = mysql_query($sql2, $conn) or die(mysql_error()); $num = mysql_num_rows($result);
if ($num > 0) // user id found { $sql = "INSERT INTO images (image_id, userName, filename) VALUES ('', '$username', '$success')"; } else if ($num == 0) // user id was not found { $message = "The User ID, '$_POST[login]' doesnt exist. <br>"; $_SESSION['$message'] = $message; }
} else { // get the date and time ini_set('date.timezone', 'Europe/London'); $now = date('Y-m-d-His'); $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
mysql_select_db($database, $conn);
//sql query to insert file name into database $sql2 = "SELECT userName FROM users WHERE userName='$username'"; $result = mysql_query($sql2, $conn) or die(mysql_error()); $num = mysql_num_rows($result);
if ($num > 0) // user id found { $sql = "INSERT INTO images (image_id, userName, filename) VALUES ('', '$username', '$success')"; } else if ($num == 0) // user id was not found { $message = "The User ID, '$_POST[login]' doesnt exist. <br>"; $_SESSION['$message'] = $message; } } if ($success) { $result = "$file uploaded successfully"; } else { $result = "Error uploading $file. Please try again."; } break;
This post has been edited by NeekWorld: 30 Jun, 2009 - 03:58 PM
If your image_id column is set to auto increment than you really don't even need to try and set a empty value ... it will do it on its own ..
If i remember right move_uploaded_file will only return a boolean result or nothing at all ... so inserting $success wont insert what you need ... you need to insert the $username.'/'.$now.$file which will hold the exact directory and file name to recall the file later ...
To only show as many images as the user has uploaded is not a problem ... use a while statement with a SELECT query that ONLY grabs images tagged with the username of your choice
CODE
$query = "SELECT filename FROM images WHERE userName='John Doe'"; $result = mysql_query($query);
while ($val = mysql_fetch_array($result)) { // do work with images ... // list($width,$height) = getimagesize($val['filename']); // echo '<img src="'. $val['filename'] .'" height="'. $height .'" width="'. $width .'"/>'; }
The while loop will only run as long as their are results still being pulled ... in other words until all the images that were successfully inserted into the database are already present ...
hope that helps ...
This post has been edited by RPGonzo: 30 Jun, 2009 - 05:49 PM
As a note ... if(is_file($image['filename'])){ would be a good idea on the page the public sees BUT if this is for the admins to see .. it would be a good idea not to use it simply to be able to see broken image links and either fix them or remove them ...
If your image_id column is set to auto increment than you really don't even need to try and set a empty value ... it will do it on its own ..
If i remember right move_uploaded_file will only return a boolean result or nothing at all ... so inserting $success wont insert what you need ... you need to insert the $username.'/'.$now.$file which will hold the exact directory and file name to recall the file later ...
To only show as many images as the user has uploaded is not a problem ... use a while statement with a SELECT query that ONLY grabs images tagged with the username of your choice
CODE
$query = "SELECT filename FROM images WHERE userName='John Doe'"; $result = mysql_query($query);
while ($val = mysql_fetch_array($result)) { // do work with images ... // list($width,$height) = getimagesize($val['filename']); // echo '<img src="'. $val['filename'] .'" height="'. $height .'" width="'. $width .'"/>'; }
The while loop will only run as long as their are results still being pulled ... in other words until all the images that were successfully inserted into the database are already present ...
hope that helps ...
ok i was thinking that i inserted the incorrect one. was going to do the
part instead of the $success in there, but shouldnt it still have at least put in the userName and id number?
QUOTE(RPGonzo @ 30 Jun, 2009 - 05:58 PM)
As a note ... if(is_file($image['filename'])){ would be a good idea on the page the public sees BUT if this is for the admins to see .. it would be a good idea not to use it simply to be able to see broken image links and either fix them or remove them ...
well i have restricted access to portions of the page. so half of the page is visible to users and half is only for admin access. i didnt post all the code what im having a problem with is displaying the images for the admin side and saving the location of the uploaded images from the user
This post has been edited by NeekWorld: 30 Jun, 2009 - 09:15 PM
So you're not getting any errors but you don't have anything in your database? Have you checked in phpmyadmin or something?
yes i checked phpMyAdmin everytime i tried to insert something, and i checked the folder i have the images sent to. the images are in the folder but the database still shows nothing. the only records in the images table are the ones i inserted manually. and i get no error message when i execute the script either from the php or the sql statement. so am puzzled at to why it is not inserting at least something into the database.
You are only Assigning Your Query to a variable, Not Executing it. look here
php
$sql = "INSERT INTO images (image_id, userName, filename) VALUES ('', '$username', '$success')"; //Here You Muse mysql_query function to Execute the Query
Try this code.
php
<?php ($sizeOK && $typeOK){ switch($_FILES['image']['error']) { case 0: // $username would normally come from a session variable $username = $_SESSION['login']; // if the subfolder doesn't exist yet, create it if (!is_dir(UPLOAD_DIR.$username)) { mkdir(UPLOAD_DIR.$username); } // make sure file of same name does not already exist if (!file_exists(UPLOAD_DIR.$username.'/'.$file)) { // get the date and time ini_set('date.timezone', 'Europe/London'); $now = date('Y-m-d-His'); // move the file to the upload folder and rename it $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
mysql_select_db($database, $conn);
//sql query to insert file name into database $sql2 = "SELECT userName FROM users WHERE userName='$username'"; $result = mysql_query($sql2, $conn) or die(mysql_error()); $num = mysql_num_rows($result);
if ($num > 0) // user id found { $sql = "INSERT INTO images (image_id, userName, filename) VALUES (NULL, '$username', '$success')"; } else if ($num == 0) // user id was not found { $message = "The User ID, '$_POST[login]' doesnt exist. <br>"; $_SESSION['$message'] = $message; }
} else { // get the date and time ini_set('date.timezone', 'Europe/London'); $now = date('Y-m-d-His'); $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
mysql_select_db($database, $conn);
//sql query to insert file name into database $sql2 = "SELECT userName FROM users WHERE userName='$username'"; $result = mysql_query($sql2, $conn) or die(mysql_error()); $num = mysql_num_rows($result);
if ($num > 0) // user id found { $sql = "INSERT INTO images (image_id, userName, filename) VALUES (NULL, '$username', '$success')"; mysql_query($sql)or die(mysql_error()); } else if ($num == 0) // user id was not found { $message = "The User ID, '$_POST[login]' doesnt exist. <br>"; $_SESSION['$message'] = $message; } } if ($success) { $result = "$file uploaded successfully"; } else { $result = "Error uploading $file. Please try again."; } break ?>
Hope it Help You
This post has been edited by noorahmad: 30 Jun, 2009 - 10:18 PM
You are only Assigning Your Query to a variable, Not Executing it. look here
php
$sql = "INSERT INTO images (image_id, userName, filename) VALUES ('', '$username', '$success')"; //Here You Muse mysql_query function to Execute the Query
Try this code.
php
<?php ($sizeOK && $typeOK){ switch($_FILES['image']['error']) { case 0: // $username would normally come from a session variable $username = $_SESSION['login']; // if the subfolder doesn't exist yet, create it if (!is_dir(UPLOAD_DIR.$username)) { mkdir(UPLOAD_DIR.$username); } // make sure file of same name does not already exist if (!file_exists(UPLOAD_DIR.$username.'/'.$file)) { // get the date and time ini_set('date.timezone', 'Europe/London'); $now = date('Y-m-d-His'); // move the file to the upload folder and rename it $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
mysql_select_db($database, $conn);
//sql query to insert file name into database $sql2 = "SELECT userName FROM users WHERE userName='$username'"; $result = mysql_query($sql2, $conn) or die(mysql_error()); $num = mysql_num_rows($result);
if ($num > 0) // user id found { $sql = "INSERT INTO images (image_id, userName, filename) VALUES (NULL, '$username', '$success')"; } else if ($num == 0) // user id was not found { $message = "The User ID, '$_POST[login]' doesnt exist. <br>"; $_SESSION['$message'] = $message; }
} else { // get the date and time ini_set('date.timezone', 'Europe/London'); $now = date('Y-m-d-His'); $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
mysql_select_db($database, $conn);
//sql query to insert file name into database $sql2 = "SELECT userName FROM users WHERE userName='$username'"; $result = mysql_query($sql2, $conn) or die(mysql_error()); $num = mysql_num_rows($result);
if ($num > 0) // user id found { $sql = "INSERT INTO images (image_id, userName, filename) VALUES (NULL, '$username', '$success')"; mysql_query($sql)or die(mysql_error()); } else if ($num == 0) // user id was not found { $message = "The User ID, '$_POST[login]' doesnt exist. <br>"; $_SESSION['$message'] = $message; } } if ($success) { $result = "$file uploaded successfully"; } else { $result = "Error uploading $file. Please try again."; } break ?>
Hope it Help You
i actually have that line in the one i was playing with after i posted the code
this is what i have now and it still doesnt execute the query
CODE
mysql_select_db($database, $conn);
//sql query to insert file name into database $sql2 = "SELECT userName FROM users WHERE userName='$username'"; $result = mysql_query($sql2, $conn) or die(mysql_error()); $num = mysql_num_rows($result);
if ($num > 0) // user id found { $sql = "INSERT INTO images (filename, userName) VALUES ('UPLOAD_DIR.$username.'/'.$now.$file', '$username')"; $result = mysql_query($sql, $conn) or die("Can't execute insert query.");
} else if ($num == 0) // user id was not found { $message = "The User ID, '$_POST[login]' doesnt exist. <br>"; $_SESSION['$message'] = $message; }
ok so i fixed that problem. i forgot to execute on both sections. but now im not getting the images to show up this is the section i have for the code. i used a dynamic table to insert the info so it will load what ever is in the record set. but all it gives me is either blank or the file location. any thoughts?
ok so i fixed that problem. i forgot to execute on both sections. but now im not getting the images to show up this is the section i have for the code. i used a dynamic table to insert the info so it will load what ever is in the record set. but all it gives me is either blank or the file location. any thoughts?
ok like i said it might help i kept coming up with the pic num instead of the pic just using the echo but got some help on here and got it sorted with the rest of the code from noormad