silverglade Posted February 21, 2012 Share Posted February 21, 2012 Hi, I am storing image names in the database, and the image in the server directory "image". I am using time() to rename the images. Basically what I do is, check the images, rename them, upload to server (or supposed to), and insert the name of the image into the database, then I use the database image name in an img src, to output the uploaded image. Here is the code. Basically it looks like the images are not uploading to the "images" directory on the server. Any help GREATLY appreciated. thanks. here is the upload code. to put image in the server renamed, and then under that the image display code taken from the database. <?php if(isset($_POST['photoUpload'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message echo '<h1>Unknown extension!</h1>'; $errors=1; } } //end if here //was else here //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file //in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname=$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessful!</h1>'; $errors=1; } else{ mysqlconnect(); // update the photo name in the database $result = mysql_query("UPDATE users SET profilePhoto='$newname' WHERE username='$currentUser'") or die(mysql_error()); echo "<h1>Photo Update Successful!</h1>"; } //replace and upload pic name to the database for later output from database. //echo out all images /* $dir = "images/"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if($file == ".") continue; if($file == "..") continue; echo "<img src='{$dir}{$file}' alt='{$file}' height='200' width='200' />"; } closedir($dh); } } */ mysqlconnect(); // SELECT FOR THE IMAGE $mysql = "SELECT profilePhoto FROM users WHERE username ='$currentUser'"; $result = mysql_query($mysql); $dir = "images/"; // if there is a result, echo out the pic using the new name of the pic if(result) { echo "<img src='{$dir}{$newname}' alt='{$newname}' height='200' width='200' />"; } if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; }//isset photo if ?> Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted February 21, 2012 Share Posted February 21, 2012 You should have error reporting set to E_ALL and display_errors on. Looking at your code, $newname doesn't include the full path, just the name. Also, where is mysqlconnect() coming from? Take a look at move_uploaded_photo Quote Link to comment Share on other sites More sharing options...
silverglade Posted February 21, 2012 Author Share Posted February 21, 2012 thank you very much for that. I used error reporting and it caught if(result) should be if($result). which would be the reason why the images did not show. But they don't show up still. I used the mysqlconnect(); to indicate to the viewers of the post that that is where I connected to the database at that point, because when I did not paste in the database connecting information at those spots, I got error messages, so I started pasting in the database connection information right in the spots where I posted "mysqlconnect();" in this post. the images are not uploading to the "images" directory on the server, and I think move_uploaded_file is to move existing files on the server. I don't think there is move_uploaded_photo? thank you for helping me to this point. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted February 21, 2012 Share Posted February 21, 2012 You use move_uploaded_file() also make sure your uploaded image sizes do not exceed your servers post_max_size and upload_max_filesize Quote Link to comment Share on other sites More sharing options...
silverglade Posted February 21, 2012 Author Share Posted February 21, 2012 awesome thank you. I FIXED IT!! hehe. Does anyone know how I can replace a user profile image and have it refresh to the new image as soon as I submit the form? The image is not refreshing, I have to reload the whole page for the new profile image to be seen. here is the code I am using to do the image replacement. if(file_exists("images/".$currentUser.".jpg")){ unlink("images/".$currentUser.".jpg"); clearstatcache(); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.