sotusotusotu Posted October 21, 2007 Share Posted October 21, 2007 Hey guys, I am trying to upload files. The uploading is no problem, but if a file with the same name is uploaded it obviously overwrites the existing one. Is there a way of checking and if the is a file already in the dir, add a letter to the end of it? <? $target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded<br />"; } else { echo "Sorry, there was a problem uploading your file."; } ?> Thanks Link to comment https://forums.phpfreaks.com/topic/74220-solved-upload-if-same-filename-exists/ Share on other sites More sharing options...
trq Posted October 21, 2007 Share Posted October 21, 2007 file_exists(). Link to comment https://forums.phpfreaks.com/topic/74220-solved-upload-if-same-filename-exists/#findComment-374888 Share on other sites More sharing options...
sotusotusotu Posted October 21, 2007 Author Share Posted October 21, 2007 I have looked up file_exists but can't seem to get it to work. Could you please give me an example? Link to comment https://forums.phpfreaks.com/topic/74220-solved-upload-if-same-filename-exists/#findComment-374896 Share on other sites More sharing options...
only one Posted October 21, 2007 Share Posted October 21, 2007 I put on an md5'd string of the date when i upload files: <?php $date = md5(date("YmdHis")); if(file_exists($date .$HTTP_POST_FILES['userfile']['name'])) { echo "error.. Please refresh the page.."; }else{ if(@copy($HTTP_POST_FILES['userfile']['tmp_name'], $date .$HTTP_POST_FILES['userfile']['name'])) { echo "successfully.."; }else{ echo "error.."; } } ?> <form enctype="multipart/form-data" action="?view=Upload" method="post" name="upload"> <input type="file" name="userfile" /> Link to comment https://forums.phpfreaks.com/topic/74220-solved-upload-if-same-filename-exists/#findComment-374899 Share on other sites More sharing options...
phpknight Posted October 21, 2007 Share Posted October 21, 2007 Just give the files your own name based on a primary key or something. PEAR also has a class where you can give every file a unique name if you do not have a database for this. It is a security risk to leave the files with the same name the user gave them. Link to comment https://forums.phpfreaks.com/topic/74220-solved-upload-if-same-filename-exists/#findComment-374910 Share on other sites More sharing options...
sotusotusotu Posted October 21, 2007 Author Share Posted October 21, 2007 All sorted. Thanks for the help guys! Link to comment https://forums.phpfreaks.com/topic/74220-solved-upload-if-same-filename-exists/#findComment-374921 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.