ciscodude Posted February 7, 2011 Share Posted February 7, 2011 Hi all, This is my very first post here, so I will try and make it count Very recently I have been working with php and I found this simple little script that allows users to upload files. I tweaked some things to only make it upload .doc files but now I have a slight problem. Every time someone uploads a word file with the same name, it gets replaced. I tried looking for other examples here on the forum, but was unable to apply it to my own script. It would be a big help if someone could provide a simple add-on to this existing script. Thanks in advance. Script: <? $locatie="upload/"; $toegestaan = "doc"; $max_size = 1000000; set_time_limit(0); if(isset($_POST['upload'])) { if(is_uploaded_file($_FILES['bestand']['tmp_name'])) { $extensie_bestand = pathinfo($_FILES['bestand']['name']); $extensie_bestand = $extensie_bestand[extension]; $extensies_toegestaan = explode(", ", $toegestaan); for($i = 0; $i < count($extensies_toegestaan); $i++) { if($extensies_toegestaan[$i] == "$extensie_bestand") { $ok = 1; } } if($ok == 1) { if($_FILES['bestand']['size']>$max_size) { echo "Het bestand is te groot, de maximale grootte is: <b>$max_size</b>"; exit; } if(!move_uploaded_file($_FILES['bestand']['tmp_name'], $locatie.$_FILES['bestand']['name'])) { echo "het bestand kan niet worden verplaatst"; exit; } echo "Het bestand ".$_FILES['bestand']['name']." is geupload<br> <a href='".$locatie."".$_FILES['bestand']['name']."' </a>"; } else { echo "Verkeerde extentie, de toegestane extensies zijn: <b>$toegestaan</b>"; } } else { echo "Het uploaden is mislukt, probeer het opnieuw"; } } ?> <title>test tittle</title> <style type="text/css"> <!-- body { margin-top: 0px; } --> </style></head> <body> <table width="1216" height="1191" border="0" cellpadding="0" cellspacing="0" background="back"> <tr> <td height="317" colspan="2"> </td> </tr> <tr> <td height="381"> </td> <td><p> </p> <form method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data"> <table width="398" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="180"> </td> <td width="218"><input type="file" name="bestand" /> <input type="submit" name="upload" value="uploaden" /></td> </tr> <tr> <td> </td> <td> </td> </tr> </table> </form></td> </tr> <tr> <td width="420"> </td> <td width="796"> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/226998-duplicate-file-names-upload-script/ Share on other sites More sharing options...
litebearer Posted February 8, 2011 Share Posted February 8, 2011 Perhaps something like this... <?PHP $file_name = "somefile.doc"; $path_to_save = "/docs"; $ok = 0; if(file_exists($path_to_save ."/". $file_name)) { $ok = 1; } while($ok==1) { $prefix = date("Ymdhis"); if(!file_exists($path . "/" . $prefix . $file_name)) { $file_name = $prefix . $file_name; $ok = 0; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/226998-duplicate-file-names-upload-script/#findComment-1171213 Share on other sites More sharing options...
ciscodude Posted February 8, 2011 Author Share Posted February 8, 2011 Thanks for the quick reply litebearer. This may sound noobish , but is there any specific location within the script I need to add this. Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/226998-duplicate-file-names-upload-script/#findComment-1171229 Share on other sites More sharing options...
ciscodude Posted February 8, 2011 Author Share Posted February 8, 2011 I tried adding it before, after, between the script, but my scripting knowledge is just to bad. If I understand correct the duplicate doc file will be moved to the docs folder..? Are there any variables I need to change..? Also I don't get the $file_name = "somefile.doc"; what function does it have..? Sorry for the uber noobish questions... Quote Link to comment https://forums.phpfreaks.com/topic/226998-duplicate-file-names-upload-script/#findComment-1171239 Share on other sites More sharing options...
litebearer Posted February 8, 2011 Share Posted February 8, 2011 somefile.doc would be the name of the you are uploading ie joes_menu. doc or mary_had_a_cow.doc you would place the code JUST BEFORE the point where you are going to save/move the file Quote Link to comment https://forums.phpfreaks.com/topic/226998-duplicate-file-names-upload-script/#findComment-1171243 Share on other sites More sharing options...
ciscodude Posted February 8, 2011 Author Share Posted February 8, 2011 The file names people will be uploading are variable. I don't know in advance what they will be. maybe someone will enter joe.doc and another joe comes and uploads joe.doc to. it could also be something else like mary.doc. Am I missing something here or is this what you are referring to..? Also I can't pinpoint the area within the script that will be saving/moving the file, where would that be..? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/226998-duplicate-file-names-upload-script/#findComment-1171258 Share on other sites More sharing options...
litebearer Posted February 8, 2011 Share Posted February 8, 2011 Ok here is example untested and un-proof-read; however I think you will get the gist of it... <?PHP if(isset($_POST['uploadedfile'] { $allowed_ext = "doc"; $path_to_save = "uploads/"; $file_name = basename( $_FILES['uploadedfile']['name']); $parts = pathinfo($file_name); $ext = $parts ['extension']; if($allowed_ext != strtolower($ext)) { echo "Invalid file type - Try again"; exit(); } $ok = 0; if(file_exists($path_to_save . $file_name)) { $ok = 1; } while($ok==1) { $prefix = date("Ymdhis"); if(!file_exists($path_to_save . $prefix . $file_name)) { $file_name = $prefix . $file_name; $ok = 0; } } $target = $path_to_save . $file_name; if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)) { if($file_name == basename( $_FILES['uploadedfile']['name'])) { echo "The file ". basename( $_FILES['uploadedfile']['name']) . " has been uploaded "; }else{ echo "The file ". basename( $_FILES['uploadedfile']['name']) . " has been uploaded and renamed " . $file_name; } } else{ echo "There was an error uploading the file, please try again!"; } }else{ ?> <form enctype="multipart/form-data" action="" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /> </form> <?PHP } ?> Quote Link to comment https://forums.phpfreaks.com/topic/226998-duplicate-file-names-upload-script/#findComment-1171280 Share on other sites More sharing options...
ciscodude Posted February 8, 2011 Author Share Posted February 8, 2011 Thanks, I tried it but got an error on line 3. Seems oke to me but I am to sleepy now, will give it a fresh look in the morning. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/226998-duplicate-file-names-upload-script/#findComment-1171324 Share on other sites More sharing options...
QuickOldCar Posted February 8, 2011 Share Posted February 8, 2011 Try this, I added a timestamp function and placed it in your code. <?php function addTimestamp($filename) { $character = "/"; $target_check = strrchr($filename,$character); if ($target_check[$character]) { $temp_filename1 = end(explode($target_check[$character],$filename)); $target = true; } else { $temp_filename1 = $filename; } $temp_filename = strtolower(trim($temp_filename1)); $timestamp = date('Y-m-d-H-i-s'); if ($target) { $new_filename = str_replace($temp_filename1, "$timestamp-$temp_filename", $filename); } else { $new_filename = "$timestamp-$temp_filename"; } return $new_filename; } $locatie="upload/"; $toegestaan = "doc"; $max_size = 1000000; set_time_limit(0); if(isset($_POST['upload'])) { if(is_uploaded_file($_FILES['bestand']['tmp_name'])) { $extensie_bestand = pathinfo($_FILES['bestand']['name']); $extensie_bestand = $extensie_bestand[extension]; $extensies_toegestaan = explode(", ", $toegestaan); for($i = 0; $i < count($extensies_toegestaan); $i++) { if($extensies_toegestaan[$i] == "$extensie_bestand") { $ok = 1; } } if($ok == 1) { if($_FILES['bestand']['size']>$max_size) { echo "Het bestand is te groot, de maximale grootte is: <b>$max_size</b>"; exit; } if(!move_uploaded_file($_FILES['bestand']['tmp_name'], $locatie.$_FILES['bestand']['name'])) { echo "het bestand kan niet worden verplaatst"; exit; } $filename = addTimestamp($locatie.$_FILES['bestand']['name']); echo "Het bestand ".$_FILES['bestand']['name']." is geupload<br> <a href='$filename'>$filename</a>"; } else { echo "Verkeerde extentie, de toegestane extensies zijn: <b>$toegestaan</b>"; } } else { echo "Het uploaden is mislukt, probeer het opnieuw"; } } ?> <title>test tittle</title> <style type="text/css"> <!-- body { margin-top: 0px; } --> </style></head> <body> <table width="1216" height="1191" border="0" cellpadding="0" cellspacing="0" background="back"> <tr> <td height="317" colspan="2"> </td> </tr> <tr> <td height="381"> </td> <td><p> </p> <form method="post" action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data"> <table width="398" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="180"> </td> <td width="218"><input type="file" name="bestand" /> <input type="submit" name="upload" value="uploaden" /></td> </tr> <tr> <td> </td> <td> </td> </tr> </table> </form></td> </tr> <tr> <td width="420"> </td> <td width="796"> </td> </tr> </table> But litebearer's code is much better for the upload Quote Link to comment https://forums.phpfreaks.com/topic/226998-duplicate-file-names-upload-script/#findComment-1171338 Share on other sites More sharing options...
litebearer Posted February 8, 2011 Share Posted February 8, 2011 BTW I forgot closing parens on the isset line - Quote Link to comment https://forums.phpfreaks.com/topic/226998-duplicate-file-names-upload-script/#findComment-1171388 Share on other sites More sharing options...
ciscodude Posted February 8, 2011 Author Share Posted February 8, 2011 I tried the code you provided quickoldcar, and it seems to work fine and even tells me that the file was successfully uploaded (with the timestamp), but in reality it just saves it under the same name without the timestamp... I also think litebearer code is much better, but I can't keep up with him. To little knowledge of programming here. At this point I am the copy/paste guy who is able to change some variables to make it work the way I want it. I tried adding )) at the end of the line: if(isset($_POST['uploadedfile']. It seems to work then (no error), but when I upload nothing happens... Quote Link to comment https://forums.phpfreaks.com/topic/226998-duplicate-file-names-upload-script/#findComment-1171481 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.