chrisuk Posted March 2, 2007 Share Posted March 2, 2007 I have been working on an upload script and it occurred to me that there would be a problem if someone tried to upload a file that already exists. Instead of producing an error message telling the user to rename their file, that i should append something to the uploaded filename to make it unique. I'd thought for the purposes of the system i am making (relatively low number of users) that a timestamp down to the second would be a sufficient measure to ensure that this problem does not occurr. At the minute, my script is as follows: <? $target_path = "../uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); $_FILES['uploadedfile']['tmp_name']; if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo '<a href="<server>/uploads/'; echo basename( $_FILES['uploadedfile']['name']). '">Click here to download your file</a>'; } else{ echo "There was an error uploading the file, please try again!"; } ?> (i have replaced the actual url with <server> for the sake of this example. How would i incorporate a timestamp into the filename before it was uploaded? I tried this: <? $target_path = "../uploads/"; $now = date("jmyGis"); $target_path = $target_path . basename( $_FILES['uploadedfile']['$now']['name']); $_FILES['uploadedfile']['tmp_name']; if(move_uploaded_file($_FILES['uploadedfile']['now']['tmp_name'], $target_path)) { echo '<a href="<server>/uploads/'; echo basename( $_FILES['uploadedfile']['$now']['name']). '">Click here to download your file</a>'; } else{ echo "There was an error uploading the file, please try again!"; } ?> but it failed. Any ideas? or is there a better way to do this? thanks Link to comment https://forums.phpfreaks.com/topic/40870-solved-make-uploaded-file-names-unique/ Share on other sites More sharing options...
papaface Posted March 2, 2007 Share Posted March 2, 2007 Your way wont work. You simply need to seperate the file extention and then re-apply it later. e.g.: <?php list($the_file_bit, $extension) = explode('.', $_FILES["upload"]["name"]); //get extention //then later $random = rand(); $tmpname = $_FILES["upload"]["tmp_name"]; $location = "files/" . $rand . "." . $extension; move_uploaded_file($tmpname,$location); ?> Link to comment https://forums.phpfreaks.com/topic/40870-solved-make-uploaded-file-names-unique/#findComment-197895 Share on other sites More sharing options...
chrisuk Posted March 2, 2007 Author Share Posted March 2, 2007 Thanks for your reply. I'm unsure how to apply this to my code though? Thanks Link to comment https://forums.phpfreaks.com/topic/40870-solved-make-uploaded-file-names-unique/#findComment-197901 Share on other sites More sharing options...
papaface Posted March 2, 2007 Share Posted March 2, 2007 Should be: <?php $target_path = "../uploads/"; $now = date("jmyGis"); list($the_file_bit, $extension) = explode('.', $_FILES["uploadedfile"]["name"]); //get extention $filename = $now . $extension; $target_path = $target_path . $filename; if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo '<a href="<server>/uploads/'; echo basename( $_FILES['uploadedfile']['$now']['name']). '">Click here to download your file</a>'; } else{ echo "There was an error uploading the file, please try again!"; } ?> Link to comment https://forums.phpfreaks.com/topic/40870-solved-make-uploaded-file-names-unique/#findComment-197918 Share on other sites More sharing options...
bwochinski Posted March 2, 2007 Share Posted March 2, 2007 Might want to add a check for "." in the filename, that could mess things a little... ( and can we please use date("U") for the timestamp?? lol ) Link to comment https://forums.phpfreaks.com/topic/40870-solved-make-uploaded-file-names-unique/#findComment-197926 Share on other sites More sharing options...
craygo Posted March 2, 2007 Share Posted March 2, 2007 explode won't work if some numnuts puts a period in the filename I have had good success with strrchr <?php <?php $file = $_FILES["upload"]["name"]; $tmpfile = $_FILES["upload"]["tmp_name"]; if(file_exists($file)){ $filename = substr($file,0,strrpos($file, ".")); $filename .= rand(); $extension = strrchr($file, "."); $newfilename = $filename.$extension; move_uploaded_file($tmpfile, $newfilename); } else { move_uploaded_file($tmpfile, $file); } ?> Ray Link to comment https://forums.phpfreaks.com/topic/40870-solved-make-uploaded-file-names-unique/#findComment-197927 Share on other sites More sharing options...
chrisuk Posted March 2, 2007 Author Share Posted March 2, 2007 Well my filename is now showing correctly with both methods but neither is moving the file to my uploads directory? i tried defining the upload directory, "$target_path = "uploads/";" and then using that like i did before in the move_uploaded_files argument ie: move_uploaded_file($tmpfile, $newfilename, $target_path); But I get Warning: Wrong parameter count for move_uploaded_file() in /home/ca3csn/public_html/COM313/tcnet/test/uploader.php on line 11 File names are right though, eg File469823764.doc Thanks for the help so far! Link to comment https://forums.phpfreaks.com/topic/40870-solved-make-uploaded-file-names-unique/#findComment-197949 Share on other sites More sharing options...
craygo Posted March 2, 2007 Share Posted March 2, 2007 I through this together to test. Take it apart and use what you want, but it works fine for me <?php if(isset($_POST['submit'])){ $dir = "images/"; $file = $_FILES['uploads']["name"]; $tmpfile = $_FILES['uploads']["tmp_name"]; if(file_exists($dir.$file)){ $filename = substr($file,0,strrpos($file, ".")); $filename .= rand(); $extension = strrchr($file, "."); $newfilename = $filename.$extension; if(!move_uploaded_file($tmpfile, $dir.$newfilename) echo "Could not upload file"; } else { echo "File uploaded"; } } else { if(!move_uploaded_file($tmpfile, $dir.$file)){ echo "Could not upload file"; } else { echo "File uploaded"; } } } else { ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data"> <p>Pictures:<br /> 1 <input type="file" name="uploads" /><br /> <input type="submit" name=submit value="Send" /> </p> </form> <?php } ?> Ray Link to comment https://forums.phpfreaks.com/topic/40870-solved-make-uploaded-file-names-unique/#findComment-197966 Share on other sites More sharing options...
chrisuk Posted March 2, 2007 Author Share Posted March 2, 2007 Thanks very much guys - I had to modify that a little ray, just due to the odd syntax error -but it's now working great. I've also removed the conditions so that the filename is always appended with a random value, as i want to write the output to a database so one variable name is better, as opposed to seperate ones for existing and new files. Most appreciated Link to comment https://forums.phpfreaks.com/topic/40870-solved-make-uploaded-file-names-unique/#findComment-197972 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.