mister_littlejeans Posted July 18, 2008 Share Posted July 18, 2008 Hi all, I have a form where a user inputs several values and then uploads a file. I'd like to change that file to either a random value or perhaps the ID field so that the files don't get overwritten. Apologies for posting a redundant question (I noticed this has already been asked) but I couldn't get their solution to work on my page and need to see how it would work on my code. Thanks in advance! Pictures of puppies/jokes/etc. for anyone who can help Code for the PHP piece... <?php //This is the directory where images will be saved $target = "uploads/"; $target = $target . basename( $_FILES['filename']['name']); //This gets all the other information from the form $name=$_POST['name']; $description=$_POST['description']; $filename=$_POST['filename']; $filename=($_FILES['filename']['name']); // Connects to your Database mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; mysql_select_db("XXX") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$filename')") ; //Writes the photo to the server if(move_uploaded_file($_FILES['filename']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 To give it a unique ID, in your MySQL table have an 'id' field (int) and make it auto_increment. Quote Link to comment Share on other sites More sharing options...
mister_littlejeans Posted July 18, 2008 Author Share Posted July 18, 2008 Thanks! I've already got the auto_increment field in the table and it works fine. I just need help renaming the files as they're uploaded so they don't get over-written Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 Oh I see. For starters, I wouldn't be putting it into the database until it's been uploaded.. Try this code: <?php //This is the directory where images will be saved $target = "uploads/"; $target = $target . basename( $_FILES['filename']['name']); //This gets all the other information from the form $name=$_POST['name']; $description=$_POST['description']; $filename=$_POST['filename']; $filename=($_FILES['filename']['name']); // Connects to your Database mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; mysql_select_db("XXX") or die(mysql_error()) ; //Check if file exists if(file_exists($target.$_FILES['filename']['name'])){ //Yup,it exists. Lets add a bit more to the name... $split = explode(".", $_FILES['filename']['name']); $uploadName = $split[0].(md5(rand(1,999)*microtime()).".".$split[1]; //this is assuming you have no other . in your filenames. }else{ $uploadName = $_FILES['filename']['name']; } //Writes the photo to the server if(move_uploaded_file($uploadName, $target)) { //Writes the information to the database mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$filename')") ; //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Hope that works. Quote Link to comment Share on other sites More sharing options...
JonnyThunder Posted July 18, 2008 Share Posted July 18, 2008 use mysql_insert_id() after your query statement to grab the ID given after your insert statement, then use that number as part of your target filename. Quote Link to comment Share on other sites More sharing options...
mister_littlejeans Posted July 18, 2008 Author Share Posted July 18, 2008 Hi ProjectFear, I get this error when using the code you suggested: Parse error: syntax error, unexpected ';' in /home/users/94/littlejeans/www/upload.moaniemoanie.com/upload.php on line 22 Quote Link to comment Share on other sites More sharing options...
mister_littlejeans Posted July 18, 2008 Author Share Posted July 18, 2008 Hi JonnyThunder, My apologies but I'm extremely new to this. Would you be able to apply your suggestion to the code provided in the original posting? Many thanks, Quote Link to comment Share on other sites More sharing options...
JonnyThunder Posted July 18, 2008 Share Posted July 18, 2008 <?php //This is the directory where images will be saved $target = "uploads/"; $target = $target . basename( $_FILES['filename']['name']); //This gets all the other information from the form $name=$_POST['name']; $description=$_POST['description']; $filename=$_POST['filename']; $filename=($_FILES['filename']['name']); // Connects to your Database mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; mysql_select_db("XXX") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$filename')") ; // Get last insert ID $lastid = mysql_insert_id(); //Writes the photo to the server if(move_uploaded_file($_FILES['filename']['tmp_name'], $target . $lastid . "_" . basename($_FILES['filename']['name']) )) { //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Roughly something like that. (Untested!) - that should put the insert ID in front of the filename. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 My bad.. Here is mine again: <?php //This is the directory where images will be saved $target = "uploads/"; $target = $target . basename( $_FILES['filename']['name']); //This gets all the other information from the form $name=$_POST['name']; $description=$_POST['description']; $filename=$_POST['filename']; $filename=($_FILES['filename']['name']); // Connects to your Database mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; mysql_select_db("XXX") or die(mysql_error()) ; //Check if file exists if(file_exists($target.$_FILES['filename']['name'])){ //Yup,it exists. Lets add a bit more to the name... $split = explode(".", $_FILES['filename']['name']); $uploadName = $split[0].(md5(rand(1,999)*microtime())).".".$split[1]; //this is assuming you have no other . in your filenames. }else{ $uploadName = $_FILES['filename']['name']; } //Writes the photo to the server if(move_uploaded_file($uploadName, $target)) { //Writes the information to the database mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$filename')") ; //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment Share on other sites More sharing options...
mister_littlejeans Posted July 18, 2008 Author Share Posted July 18, 2008 Hi JonnyThunder, When I use your code, the following happens: If I upload a file called TREES.JPG, it goes into the folder TREES.JPG24_TREES.JPG (I've already tested 23 files...) Any chance you can help me re-name it do there's only one name + extension? (e.g. 24_TREES.JPG) Also, the file is correctly named in the folder, but the $_POST call is still inserting the original filename. Any help making it so the database update and the folder both have identical re-named files would be a huge help. THANKS SO MUCH!! Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 If you try mine you'll have to change the query to this: mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$uploadName')") ; Sorry... It's 2:11AM. Quote Link to comment Share on other sites More sharing options...
mister_littlejeans Posted July 18, 2008 Author Share Posted July 18, 2008 Hi ProjectFear, I get an error with the code you suggested (Sorry, there was a problem uploading your file.) Thanks very much for helping. Any ideas what might be wrong? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 Ha. Man am I on fire. Okay try it like this, otherwise... I'm going to bed. <?php //This is the directory where images will be saved $target = "uploads/"; $target = $target . basename( $_FILES['filename']['name']); //This gets all the other information from the form $name=$_POST['name']; $description=$_POST['description']; $filename=$_POST['filename']; $filename=($_FILES['filename']['name']); // Connects to your Database mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; mysql_select_db("XXX") or die(mysql_error()) ; //Check if file exists if(file_exists($target.$_FILES['filename']['tmp_name'])){ //Yup,it exists. Lets add a bit more to the name... $split = explode(".", $_FILES['filename']['tmp_name']); $uploadName = $split[0].(md5(rand(1,999)*microtime())).".".$split[1]; //this is assuming you have no other . in your filenames. }else{ $uploadName = $_FILES['filename']['tmp_name']; } //Writes the photo to the server if(move_uploaded_file($uploadName, $target)) { //Writes the information to the database mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$uploadName')") ; //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment Share on other sites More sharing options...
mister_littlejeans Posted July 18, 2008 Author Share Posted July 18, 2008 Hi ProjectFear, Thanks so much! The file is successfully renamed in the database, but the original filename still appears in the folder after being uploaded. Any ideas? We're getting extremely close Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 Your sure? Well I can't see why. I'm going to bed, someone should surely find why though. Quote Link to comment Share on other sites More sharing options...
mister_littlejeans Posted July 18, 2008 Author Share Posted July 18, 2008 Any other ideas? Currently, we have one submission that renames the file going into the folder, but not the data going into the database. We have another submission that renames the data going into the database but not the file going into the folder. ...so close and yet so far. Any other ideas are most welcome. Thanks in advance! Quote Link to comment Share on other sites More sharing options...
mister_littlejeans Posted July 18, 2008 Author Share Posted July 18, 2008 Hi everyone, Just to let you know that I think I've figured it out. Thanks to ProjectFear and JonnyThunder - I used both of your examples as inspiration. In the end, I went for a random generator instead of incremental IDs, since it felt a bit more safe in a race situation. Here's the code I ended up with. It appears to work perfectly. <?php //This is the directory where images will be saved $target = "uploads/"; //This gets all the other information from the form $name=$_POST['name']; $description=$_POST['description']; $filename=$_POST['filename']; $filename=($_FILES['filename']['name']); // Connects to your Database mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; mysql_select_db("XXX") or die(mysql_error()) ; // Get last insert ID $lastid = md5(uniqid()) . "_" . basename($_FILES['filename']['name']); //Writes the photo to the server if(move_uploaded_file($_FILES['filename']['tmp_name'], $target . $lastid )) { //Writes the information to the database mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$lastid')") ; //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 19, 2008 Share Posted July 19, 2008 Haha, I think thats where I went wrong. This would be my code, fixed. I hope: <?php //This is the directory where images will be saved $target = "uploads/"; $target = $target . basename( $_FILES['filename']['name']); //This gets all the other information from the form $name=$_POST['name']; $description=$_POST['description']; $filename=$_POST['filename']; $filename=($_FILES['filename']['name']); // Connects to your Database mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; mysql_select_db("XXX") or die(mysql_error()) ; //Check if file exists if(file_exists($target.$_FILES['filename']['tmp_name'])){ //Yup,it exists. Lets add a bit more to the name... $split = explode(".", $_FILES['filename']['tmp_name']); $uploadName = $split[0].(md5(rand(1,999)*microtime())).".".$split[1]; //this is assuming you have no other . in your filenames. }else{ $uploadName = $_FILES['filename']['tmp_name']; } //Writes the photo to the server if(move_uploaded_file($_FILES['filename']['tmp_name'], $uploadName)) { //Writes the information to the database mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$uploadName')") ; //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> 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.