A JM Posted May 11, 2009 Share Posted May 11, 2009 I want to test for the existence of a file in the directory before saving. If the file already exists then I want to increment the file name by appending a number to end of the file name, then save the file. The caveat for this file save routine is that if this process has happend before, filename1.pdf exists then the new file should increment one more number to filename2.pdf, etc... I'm not familiar with loops and wanted to make sure that there isn't an easier/better way to do this, any ideas or thoughts? I hacked this up and I think will work???? $fnamecnt= 1; $newfname = false; $fname = preg_replace('/\..*$/','',$fname); //filename no extension $ext = substr($fname, strpos($fname,'.'), strlen($fname)-1); //file extension while ( $newfname = false ) { //Test if file exists and rename if needed if (!file_exists($uploaddir . $fname . $ext)) { //Move file to new destination if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $fname . $ext)) { // uploaded file was moved and renamed succesfuly. Display a message. echo "Upload successful!"; } else { echo "Error while uploading the file, Please contact the webmaster."; unset($fname . $ext); } $newfname = true; } else { $fname = $fname . $fnamcnt; } $fnamecnt = $fnamcnt +1; } Thanks. A JM, Quote Link to comment https://forums.phpfreaks.com/topic/157730-solved-if-file_exists-loop-question/ Share on other sites More sharing options...
premiso Posted May 11, 2009 Share Posted May 11, 2009 while ( $newfname = false ) { Will always be true. You need to use == for a comparison operator. Alternatively, you can just do a break; when the file_exists and it will exit out of the loop. Quote Link to comment https://forums.phpfreaks.com/topic/157730-solved-if-file_exists-loop-question/#findComment-831899 Share on other sites More sharing options...
RussellReal Posted May 11, 2009 Share Posted May 11, 2009 $file = 'kenny'; $ext = 'pdf'; $count = 0; if (file_exists($file.'.'.$ext)) { while (file_exists("{$file}{$count}.{$ext}")) { $count++; } // save file with name "{$file}{$count}.{$ext}" } else { // save file with correct name } Quote Link to comment https://forums.phpfreaks.com/topic/157730-solved-if-file_exists-loop-question/#findComment-831900 Share on other sites More sharing options...
A JM Posted May 11, 2009 Author Share Posted May 11, 2009 while ( $newfname = false ) { Will always be true. You need to use == for a comparison operator. Alternatively, you can just do a break; when the file_exists and it will exit out of the loop. With regard to the Break; comment do you mean to not check for $newfilename = true; instead just use break;? I'll still need the variable though correct (while ( $newfname == false )) otherwise there's nothing for it to loop against? Thanks for the post premiso but I'm working in PHP and not that familiar with javascript. A JM, Quote Link to comment https://forums.phpfreaks.com/topic/157730-solved-if-file_exists-loop-question/#findComment-831992 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.