phpwannabe25 Posted August 25, 2009 Share Posted August 25, 2009 Hello! I am uploading photos to an images folder in my database, and savin the image name in a table. In order to prevent from duplicating the name of the image, i have included a random image name function, which assigns each image with a new randomly generated number. This is working and is storing the new name in the table perfectly. however, the problem is that the image that is uploaded to the images folder, still has the origianl name and is not then recognized when i try and call the image to the page.. the code so far is..... any suggestions are appreciated. <?php //This function separates the extension from the rest of the file name and returns it function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } //This applies the function to our file $ext = findexts ($_FILES['uploaded']['name']) ; //This line assigns a random number to a variable. $ran = rand () ; //This takes the random number you generated and adds a . on the end, so it is ready of the file extension to be appended. $ran2 = $ran."."; //This is the directory where images will be saved $target = "images/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $name=$_POST['name']; $email=$_POST['email']; $phone=$_POST['phone']; $pic=($_FILES['photo']['name']); // Connects to your Database mysql_connect("xxx", "yyy", "zzz") or die(mysql_error()) ; mysql_select_db("sssss") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$ran2.$ext')") ; //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file has been uploaded as ".$ran2.$ext; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171755-uploading-storing-image-problem/ Share on other sites More sharing options...
ignace Posted August 25, 2009 Share Posted August 25, 2009 function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } is the same as: $ext = pathinfo($filename, PATHINFO_EXTENSION); it's not $target . basename($_FILES[..]) but: $target = $target . $ran2 . $ext; mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$ran2.$ext')") ; $ran2.$ext is wrong because it will insert filename..extension P.S. $pic=($_FILES['photo']['name']); $pic is never used Quote Link to comment https://forums.phpfreaks.com/topic/171755-uploading-storing-image-problem/#findComment-905662 Share on other sites More sharing options...
phpwannabe25 Posted August 25, 2009 Author Share Posted August 25, 2009 thanks for your help! I have made the changes there and now i have it workin so that the file is renamed, it is stored in the images folder and the table with the new name. And i have tested it by adding a different image with the same name and both images are renamed and recalled back correctly. my only concern is now, is that the file is not being saved with an extension, or any extension at all it still works, however it would be good to get the extension saved also. <?php //This function separates the extension from the rest of the file name and returns it $ext = pathinfo($filename, PATHINFO_EXTENSION); //This applies the function to our file $ext = findexts ($_FILES['uploaded']['name']) ; //This line assigns a random number to a variable. You could also use a timestamp here if you prefer. $ran = rand () ; //This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended. $ran2 = $ran."."; //This is the directory where images will be saved $target = "images/"; $target = $target . $ran2 . $ext; //This gets all the other information from the form $name=$_POST['name']; $email=$_POST['email']; $phone=$_POST['phone']; $pic=($_FILES['photo']['name']); // Connects to your Database mysql_connect("xxxx", "zzzz", "yyyy") or die(mysql_error()) ; mysql_select_db("hhhhhl") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$ran2')") ; //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file has been uploaded as ".$ran2.$ext; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171755-uploading-storing-image-problem/#findComment-905676 Share on other sites More sharing options...
ignace Posted August 25, 2009 Share Posted August 25, 2009 //This function separates the extension from the rest of the file name and returns it $ext = pathinfo($filename, PATHINFO_EXTENSION); //This applies the function to our file $ext = findexts ($_FILES['uploaded']['name']) ; should be: //This function separates the extension from the rest of the file name and returns it $ext = pathinfo($_FILES['uploaded']['name'], PATHINFO_EXTENSION); Quote Link to comment https://forums.phpfreaks.com/topic/171755-uploading-storing-image-problem/#findComment-905713 Share on other sites More sharing options...
phpwannabe25 Posted August 25, 2009 Author Share Posted August 25, 2009 thanks very much for your help, much appreciated! i am tryin to integrate that php code in to a longer signup document. the code i am using at the moment, says 'your image has been saved as....etc' //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file has been uploaded as ".$ran2.$ext; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } however, what i am hopin for , is that the only time a message will appear is that there has been a problem, and otherwsie the script will run.. so basically i am tryin to get it as somthign like //Writes the photo to the server if!(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } Quote Link to comment https://forums.phpfreaks.com/topic/171755-uploading-storing-image-problem/#findComment-905723 Share on other sites More sharing options...
ignace Posted August 25, 2009 Share Posted August 25, 2009 if (!move_uploaded_file($_FILES['photo']['tmp_name'], $target)) Quote Link to comment https://forums.phpfreaks.com/topic/171755-uploading-storing-image-problem/#findComment-906127 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.