NathanLedet Posted August 22, 2008 Share Posted August 22, 2008 I have an upload script and I'm wondering how I check to see if the file exists on the server already, and if so, append a 01 or 02 (if there is a 01). Thanks for any advice Quote Link to comment Share on other sites More sharing options...
FVxSF Posted August 22, 2008 Share Posted August 22, 2008 You could use a while loop. Split the extension from the file name then append an integer and the extension I didn't test this but i think you'll get the general idea: <?php // This should keep checking until the file doesn't exists while( file_exists($YourFile) { $Count += 1; $YourImageFile = $FileName . $Count . $Extension; } ?> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 22, 2008 Share Posted August 22, 2008 This may be long, but it should work... $file = 'location/of/your/file.jpg'; $pos = strrpos($file,'.'); $ext = substr($file,$pos); $dir = strrpos($file,'/'); $dr = substr($file,0,($dir+1)); $arr = explode('/',$file); $fName = trim($arr[(count($arr) - 1)],$ext); $exist = FALSE; $i = 0; while(!$exist){ if(file_exists($file.$i)||file_exists($file)){ continue; }else{ $exist = TRUE; $file = $dr.$fName.$i.$ext; } $i++; } echo $file; // Upload Script Here Quote Link to comment Share on other sites More sharing options...
uberweiss Posted October 24, 2008 Share Posted October 24, 2008 how do i then rename the file before copying? automatically? Quote Link to comment Share on other sites More sharing options...
uberweiss Posted October 24, 2008 Share Posted October 24, 2008 i mean, i can't get either of them to work... what am i doing wrong... i made this: where should i paste in the code? <?php $path = "C:\web\www-root\www.eurogiro.com\html\joomla\upload";//Sti til mappen filen skal uploades i! $filename = "coc.xls";//Filens navn if(count($_POST)>0)//Der bliver postet til scriptet { $file = $_FILES['file']; if(move_uploaded_file($file['tmp_name'], $path."/".$filename)) { echo 'The file has been succesfully uploaded'; } else { echo 'The file wasnt uploaded. Please try again'; } } else { $form = '<form enctype="multipart/form-data" action="" method="post"> <input type="file" name="file" value=""><br> <input type="submit" name="submit" value="Upload"> </form>'; echo $form; } echo "</body> </html>"; ?> Quote Link to comment Share on other sites More sharing options...
feidakila Posted October 24, 2008 Share Posted October 24, 2008 rename("/path/older_file.ext", "/path/new_filename.ext"); Quote Link to comment Share on other sites More sharing options...
feidakila Posted October 24, 2008 Share Posted October 24, 2008 Check if the file exists before uploading, if exists rename with the new generated name. I guess this should work $file_path = $path."/".$filename if (file_exists($filepath)) { /*generate the $new_file_path, with the code given you before*/ rename($filepath,$new_file_path); } if(move_uploaded_file($file['tmp_name'], $path."/".$filename)) { echo 'The file has been succesfully uploaded'; } else { echo 'The file wasnt uploaded. Please try again'; } Quote Link to comment Share on other sites More sharing options...
uberweiss Posted October 24, 2008 Share Posted October 24, 2008 thanks alot... the only problem now is that i hardcode the "new file name" i would like if the file exists call the file filename1 and if filename1 exists then filename2 and if filename2 exist then filename3 and so on... 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.