sourav.network Posted December 11, 2010 Share Posted December 11, 2010 Hi, I need some help in saving the filename along with the extension in the database after i upload the file. Current Scenario: I am able to upload the file successfully and save the form details in the database. Problems: When file uploaded with same file name it overwrites the old file. How do i give the file a unique name when uploading the file and then saving the form details along with the new file name given? CODE USED TO SAVE THE FILE TO MYSQL AND UPLOAD THE FILE: <?php //This is the directory where images will be saved $target = "image/"; $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("localhost", "data_admin2", "data25") or die(mysql_error()) ; mysql_select_db("Resumebank") or die(mysql_error()) ; //Writes the photo to the server move_uploaded_file($_FILES['photo']['tmp_name'], $target); //Writes the information to the database mysql_query("INSERT INTO `employee` VALUES ('$name', '$email', '$phone', '$pic')") ; echo "The file has been uploaded, and your information has been added to the directory"; ?> What should I do? Please help! Quote Link to comment https://forums.phpfreaks.com/topic/221305-need-help-wile-uploading-a-file-unique-name-required/ Share on other sites More sharing options...
MMDE Posted December 11, 2010 Share Posted December 11, 2010 Isn't it just to use the rename function? http://php.net/manual/en/function.rename.php Just run a rename on the $target after it has been moved. At least worked for me when I did this: $newname='lol.txt'; if($_FILES){ if(move_uploaded_file($_FILES['userfile']['tmp_name'], basename($_FILES['userfile']['name']))){ echo 'You successfully uploaded the file: '.$_FILES['userfile']['name']; if(rename($_FILES['userfile']['name'],$newname)){ echo ' and renamed it to '.$newname; }else{ echo ' and failed rename it to '.$newname; } echo '.'; }else{ echo 'ERROR'; } } Quote Link to comment https://forums.phpfreaks.com/topic/221305-need-help-wile-uploading-a-file-unique-name-required/#findComment-1145699 Share on other sites More sharing options...
jamesxg1 Posted December 11, 2010 Share Posted December 11, 2010 <?php //This is the directory where images will be saved $target = "image/"; $target = $target . md5() . '-' . 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("localhost", "data_admin2", "data25") or die(mysql_error()) ; mysql_select_db("Resumebank") or die(mysql_error()) ; //Writes the photo to the server move_uploaded_file($_FILES['photo']['tmp_name'], $target); //Writes the information to the database mysql_query("INSERT INTO `employee` VALUES ('$name', '$email', '$phone', '$pic')") ; echo "The file has been uploaded, and your information has been added to the directory"; ?> James. Quote Link to comment https://forums.phpfreaks.com/topic/221305-need-help-wile-uploading-a-file-unique-name-required/#findComment-1145711 Share on other sites More sharing options...
Anti-Moronic Posted December 11, 2010 Share Posted December 11, 2010 <?php //This is the directory where images will be saved $target = "image/"; $target = $target . md5() . '-' . 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("localhost", "data_admin2", "data25") or die(mysql_error()) ; mysql_select_db("Resumebank") or die(mysql_error()) ; //Writes the photo to the server move_uploaded_file($_FILES['photo']['tmp_name'], $target); //Writes the information to the database mysql_query("INSERT INTO `employee` VALUES ('$name', '$email', '$phone', '$pic')") ; echo "The file has been uploaded, and your information has been added to the directory"; ?> James. Don't forget, $pic is also using the filename. Currently, the wrong filename and also a filename which could easily conflict with somebody else's. Quote Link to comment https://forums.phpfreaks.com/topic/221305-need-help-wile-uploading-a-file-unique-name-required/#findComment-1145724 Share on other sites More sharing options...
MMDE Posted December 11, 2010 Share Posted December 11, 2010 Isn't it just to use the rename function? http://php.net/manual/en/function.rename.php Just run a rename on the $target after it has been moved. At least worked for me when I did this: $newname='lol.txt'; if($_FILES){ if(move_uploaded_file($_FILES['userfile']['tmp_name'], basename($_FILES['userfile']['name']))){ echo 'You successfully uploaded the file: '.$_FILES['userfile']['name']; if(rename($_FILES['userfile']['name'],$newname)){ echo ' and renamed it to '.$newname; }else{ echo ' and failed rename it to '.$newname; } echo '.'; }else{ echo 'ERROR'; } } Oh I forgot to add that it's just to check if the name exists and if it does give it a new random one, and loop check if it exists till you are sure it does not! o.o random file name: $randomfilename='pic'.rand(0,999999).'.jpg'; for example, but you would have to make sure the extension is right, I think you can get that from the $_FILE variable. Quote Link to comment https://forums.phpfreaks.com/topic/221305-need-help-wile-uploading-a-file-unique-name-required/#findComment-1145728 Share on other sites More sharing options...
sourav.network Posted December 11, 2010 Author Share Posted December 11, 2010 Thanks guys! But its not working for me. When i upload it, it doesnt rename the file...... Sorry, i dont know where am i wrong.. Please help guys! Quote Link to comment https://forums.phpfreaks.com/topic/221305-need-help-wile-uploading-a-file-unique-name-required/#findComment-1145914 Share on other sites More sharing options...
QuickOldCar Posted December 12, 2010 Share Posted December 12, 2010 Try adding timestamp, because there could only be one. Try this. <?php //This is the directory where images will be saved $timestamp = time(); $target = "image/"; $target = $target.$timestamp. '-' . basename( $_FILES['photo']['name']); //This gets all the other information from the form $name=$_POST['name']; $email=$_POST['email']; $phone=$_POST['phone']; $pic = $timestamp. '-' . $_FILES['photo']['name']; // Connects to your Database mysql_connect("localhost", "data_admin2", "data25") or die(mysql_error()) ; mysql_select_db("Resumebank") or die(mysql_error()) ; //Writes the photo to the server move_uploaded_file($_FILES['photo']['tmp_name'], $target); //Writes the information to the database mysql_query("INSERT INTO `employee` VALUES ('$name', '$email', '$phone', '$pic')") ; echo "The file has been uploaded, and your information has been added to the directory"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/221305-need-help-wile-uploading-a-file-unique-name-required/#findComment-1146026 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.