JustinMs66@hotmail.com Posted November 8, 2006 Share Posted November 8, 2006 ok so i have a working awesome uploads script, but it needs something. this is what i want it to have:it checks the "/uploads/" directory for the name of the file it's uploading. IF it dosn't exist, then continue. if it DOES exist, then rename it to the file name + "_1" and then check if THAT exists, and if it DOES, then take away the "_1" and put in "_2" and keep going until it dosn't exist, and then upload with that file name.here is my current code[code]<?php//check for types that i DON"T want to have uploaded:$bad_types = array('application/x-php');//if it is a bad file, then say "F*** youif( in_array( $_FILES['uploadedfile']['type'], $bad_types ) ){ echo "That File type is not supported.";}else{//otherwise continue uploading$target_path = "uploads/";$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);//uploads the file if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {//display the link to the file:echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded. here is the link to your file: <a href='uploads/". basename( $_FILES['uploadedfile']['name']). "'>". basename( $_FILES['uploadedfile']['name']). "</a>"; } else{ echo "There was an error uploading the file, please try again!"; }[/code]can anyone get me started? Quote Link to comment Share on other sites More sharing options...
heckenschutze Posted November 8, 2006 Share Posted November 8, 2006 Untested, but my logic seems sound to me :)[code]<?phpfunction CheckFile($strDirectory, $strFile) // $strDirectory MUST include a trailing slash!{ if($strFile && file_exists($strFile)) { $x = 0; do { $x++; } while(file_exists($strDirectory . $x . "_" . $strFile)); $strNewName = $x . "_" . $strFile; }else{ return $strFile; // unique filename } return $strNewName;}?>[/code]Now what it does is; You tell the function which filename your looking for (and in what directory) and it returns the closest filename to what you specified (which is unique)...eg:[code]$strUniqueFilename = CheckFile("uploads/", $_FILES['uploadedfile']['name']);[/code]Hope that helps :) Quote Link to comment Share on other sites More sharing options...
Nicklas Posted November 8, 2006 Share Posted November 8, 2006 If you use PHP5, you could do like this:[code=php:0]<?phpfunction scanfiles($dir, $filename, $nr = 1) {$tmp_name = basename($filename, '.php') . "_$nr.php"; $file = scandir($dir); // If the file doesn´t exist, return the new filename. if (!in_array($tmp_name, $file)) { return $tmp_name; // If the file exist, search again... } else { $nr++; return scanfiles($dir, $filename, $nr); } }// Some code here...$dir = "uploads"; // no trailing slash!// And the new filename is... ?$new_filename = scanfiles($dir, $_FILES['uploadedfile']['name']);$target_path = "$dir/$new_filename";if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { // Do your stuff here... }?>[/code] Quote Link to comment Share on other sites More sharing options...
heckenschutze Posted November 9, 2006 Share Posted November 9, 2006 If you had say 2 billion files in the directory, its going to get very slow using Nicklas's method. Quote Link to comment Share on other sites More sharing options...
JustinMs66@hotmail.com Posted November 9, 2006 Author Share Posted November 9, 2006 [quote author=heckenschutze link=topic=114322.msg465197#msg465197 date=1163032155]If you had say 2 billion files in the directory, its going to get very slow using Nicklas's method.[/quote]well:1) i'm using PHP42) i only have a couple thousand files, so that shouldn't be a problem anywho.anyway, thank you for your code and i will try it out!thanks! :) Quote Link to comment Share on other sites More sharing options...
Nicklas Posted November 9, 2006 Share Posted November 9, 2006 [quote author=heckenschutze link=topic=114322.msg465197#msg465197 date=1163032155]If you had say 2 billion files in the directory, its going to get very slow using Nicklas's method.[/quote]2 billion files in one directory... not possible!This method slow? Well, I tried the script in a few folders with 5-7000 files in each and it spits back a new filname in around 0.04xxx seconds on my old laptop.To make it compatible with PHP4, just change[code]$file = scandir($dir);[/code]to this:[code]if ($handle = opendir($dir)) { while (false !== ($cont = readdir($handle))) if (!is_dir($dir)) $file[] = $cont;} closedir($handle);[/code] Quote Link to comment Share on other sites More sharing options...
heckenschutze Posted November 9, 2006 Share Posted November 9, 2006 [quote author=Nicklas link=topic=114322.msg465230#msg465230 date=1163037856][quote author=heckenschutze link=topic=114322.msg465197#msg465197 date=1163032155]If you had say 2 billion files in the directory, its going to get very slow using Nicklas's method.[/quote]2 billion files in one directory... not possible!This method slow? Well, I tried the script in a few folders with 5-7000 files in each and it spits back a new filname in around 0.04xxx seconds on my old laptop.To make it compatible with PHP4, just change[code]$file = scandir($dir);[/code]to this:[code]if ($handle = opendir($dir)) { while (false !== ($cont = readdir($handle))) if (!is_dir($dir)) $file[] = $cont;} closedir($handle);[/code][/quote]Yes its possible, You don't need to scan through ALL of the files just to see if one file exists, your loading the [b]entire[/b] directory contents into [b]memory[/b]... very un-resource friendly.Eg if you had 7000 files, with an average of 60 bytes per filename, thats [b]420kb[/b] of memory already gone. It may not seem a lot, but multiply that by several thousand users using your site at one time... Thats why you must be aware that you can't just go haywire with arrays with 7000 + elements. Quote Link to comment 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.