Drongo_III Posted April 30, 2011 Share Posted April 30, 2011 Hi guys Still learning php and have what is probably quite a newbie question! I have an upload script (below) that uploads images but I can't work out how to change the name of the file before it's moved from it's temporary location to the 'uploads' directory. I've tried a few things but nothing seems to work and I'm a bit stuck. The ultimate goal is to time stamp the new image name so that it will be unique but the first step is to figure out how to change the name. Any help would be much appreciated! Thanks, Drongo The script I'm using is as follows: if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 200000000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } Quote Link to comment https://forums.phpfreaks.com/topic/235231-changing-file-name-on-upload/ Share on other sites More sharing options...
Fadion Posted April 30, 2011 Share Posted April 30, 2011 Instead of $_FILES['file']['name'], you can use whatever you want. Take this example: <?php $file = $_FILES['file']['name']; //get the file extension, we'll need it to construct the new filename $ext = pathinfo($file, PATHINFO_EXTENSION); $new_filename = "any_filename_you_want.$ext"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/235231-changing-file-name-on-upload/#findComment-1208831 Share on other sites More sharing options...
Drongo_III Posted May 1, 2011 Author Share Posted May 1, 2011 Hi Guilty Thanks for such a quick reply. Sorry to labour the point. I've incorporated your code into mine. Is what I've done correct? It works fine but I want to be sure I'm doing it the right way. <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 200000000)) { $file = $_FILES['file']['name']; //get the file extension, we'll need it to construct the new filename $ext = pathinfo($file, PATHINFO_EXTENSION); $new_filename = "any_filename_you_want2222332.$ext"; move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_filename); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } else { echo "Invalid file"; } // ############ THIS IS THE DATABASE PART ############### $title = $_POST['title']; $body = $_POST['body']; $con = mysql_connect("localhost","zzzzzzzz_testing","RB.x;)c*=a4z"); if (!$con) { die('Could not connect: ' . mysql_error()); } // Create table mysql_select_db("zzzzzzzz_newtest", $con); mysql_query("INSERT INTO page_stuff (body_text, title, image) VALUES ( '$body', '$title', '$image_name')"); mysql_close($con); ?> Instead of $_FILES['file']['name'], you can use whatever you want. Take this example: <?php $file = $_FILES['file']['name']; //get the file extension, we'll need it to construct the new filename $ext = pathinfo($file, PATHINFO_EXTENSION); $new_filename = "any_filename_you_want.$ext"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/235231-changing-file-name-on-upload/#findComment-1208839 Share on other sites More sharing options...
Fadion Posted May 1, 2011 Share Posted May 1, 2011 Exactly! Just that this part: <?php move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_filename); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; ?> could be put in a conditional, so that you know if the upload works: <?php if (move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_filename)) { echo 'Stored in: upload/' . $new_filename; } else { echo 'An error occurred while uploading.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235231-changing-file-name-on-upload/#findComment-1208846 Share on other sites More sharing options...
Drongo_III Posted May 1, 2011 Author Share Posted May 1, 2011 Fabulous! Thank you so much for your help! It all makes a lot more sense now Exactly! Just that this part: <?php move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_filename); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; ?> could be put in a conditional, so that you know if the upload works: <?php if (move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_filename)) { echo 'Stored in: upload/' . $new_filename; } else { echo 'An error occurred while uploading.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235231-changing-file-name-on-upload/#findComment-1208852 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.