jjmusicpro Posted July 23, 2008 Share Posted July 23, 2008 Right now i have an uploading script working, a basic one. However, if someone uploads a file with the same name, it will overwirte the previous one. How can i make it so it checks to see if there is a file with the same name, if so, just put a number on the end that increments or something.? Thanks for your help $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded.<br>"; echo "<img src=\"/uploads/".basename( $_FILES['uploadedfile']['name'])."\">"; } else{ echo "There was an error uploading the file, please try again!"; } Quote Link to comment https://forums.phpfreaks.com/topic/116115-upload-file-change-name-if-file-has-same-name/ Share on other sites More sharing options...
vbnullchar Posted July 23, 2008 Share Posted July 23, 2008 use this : http://www.php.net/file_exists Quote Link to comment https://forums.phpfreaks.com/topic/116115-upload-file-change-name-if-file-has-same-name/#findComment-597115 Share on other sites More sharing options...
jjmusicpro Posted July 23, 2008 Author Share Posted July 23, 2008 i dont know how to reloop it back through, and keep checking if that file name already exsists. so if there is already a test.gif, it would go to test.gif1, however, if there is already a test.gif1..i need it to go to the next number. or cant i add some weird time hash on the end of the file name? Quote Link to comment https://forums.phpfreaks.com/topic/116115-upload-file-change-name-if-file-has-same-name/#findComment-597117 Share on other sites More sharing options...
vbnullchar Posted July 23, 2008 Share Posted July 23, 2008 like this... (i have not tried executing this code.. ) <?php $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); $i = 0; while(file_exists($target_path).$i) { $i++; } if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path.$i)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded.<br>"; echo "<img src=\"/uploads/".basename( $_FILES['uploadedfile']['name'])."\">"; } else{ echo "There was an error uploading the file, please try again!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/116115-upload-file-change-name-if-file-has-same-name/#findComment-597120 Share on other sites More sharing options...
unkwntech Posted July 23, 2008 Share Posted July 23, 2008 Optionaly you could store the information about the file in a database and apend it's id to the filename. Quote Link to comment https://forums.phpfreaks.com/topic/116115-upload-file-change-name-if-file-has-same-name/#findComment-597127 Share on other sites More sharing options...
jjmusicpro Posted July 23, 2008 Author Share Posted July 23, 2008 the above code just times out now.. Quote Link to comment https://forums.phpfreaks.com/topic/116115-upload-file-change-name-if-file-has-same-name/#findComment-597145 Share on other sites More sharing options...
vbnullchar Posted July 23, 2008 Share Posted July 23, 2008 sorry, please use this code.. <?php while(file_exists($target_path.$i)) { ?> Quote Link to comment https://forums.phpfreaks.com/topic/116115-upload-file-change-name-if-file-has-same-name/#findComment-597158 Share on other sites More sharing options...
jjmusicpro Posted July 24, 2008 Author Share Posted July 24, 2008 sorry, please use this code.. <?php while(file_exists($target_path.$i)) { ?> where do i put that? Quote Link to comment https://forums.phpfreaks.com/topic/116115-upload-file-change-name-if-file-has-same-name/#findComment-598120 Share on other sites More sharing options...
jjmusicpro Posted July 24, 2008 Author Share Posted July 24, 2008 so this is what i have but its not changing name <? $target_path = "uploads/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); $i = 0; while(file_exists($target_path.$i)) { $i++; } if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path.$i)) { echo "<img src=\"/uploads/".basename( $_FILES['uploadedfile']['name'])."\"><br><br>"; echo "<b>Direct Link</b><br>"; echo "<textarea cols=\"50\" rows=\"3\">http://www.blintool.com/uploads/".basename( $_FILES['uploadedfile']['name'])."</textarea>"; echo "<br><br>"; echo "<b>Myspace / FaceBook / Hi5 / Orkut Links</b><br>"; echo "<textarea cols=\"50\" rows=\"3\"><a href=\"http://www.blingtool.com\"><img src=\"http://www.blingtool.com/uploads/".basename( $_FILES['uploadedfile']['name'])."></a></textarea>"; } else{ echo "There was an error uploading the file, please try again!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/116115-upload-file-change-name-if-file-has-same-name/#findComment-598121 Share on other sites More sharing options...
jjmusicpro Posted July 24, 2008 Author Share Posted July 24, 2008 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/116115-upload-file-change-name-if-file-has-same-name/#findComment-598218 Share on other sites More sharing options...
jjmusicpro Posted July 24, 2008 Author Share Posted July 24, 2008 I looked in my file directory and what its doing is, adding the number on the end of the file name, so if i uploaded me.gif already, its making a me.gif1 not me1.gif. Do you think it would be better to just add a 10 digit random number to the end? Quote Link to comment https://forums.phpfreaks.com/topic/116115-upload-file-change-name-if-file-has-same-name/#findComment-598554 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.