PerHaPs Posted January 8, 2007 Share Posted January 8, 2007 Hi,This bit of code is supposed to detect which filenames have been used alreadyout of a possible 121.jpg,2.jpg ...... 12.jpg.If 1.jpg does not exist,$filenum_next should be '1'.THE CODEfor($i=1;$i<=12;$i++){if($breaker)break;$filename_to_check=$i.'.jpg';$opendir=opendir($path_thumbs); while (false !== ($file=readdir($opendir))) { if($breaker)break; if($file != '..' && $file !='.' && $file !='') { if(!file_exists($filename_to_check)) { $filenum_next=$i; $breaker=1; } } } }Instead it acts as if the directory is empty. It suggests 2.jpg should be the next filename. Next time it assigns 2.jpg again and overwrites the file.Any thoughts? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/33384-file_exists-problem/ Share on other sites More sharing options...
Jessica Posted January 8, 2007 Share Posted January 8, 2007 Please use the code tags (The # button).Where is $path_thumbs defined? same for $breaker?Does it suggest 2 if there are NO files? Quote Link to comment https://forums.phpfreaks.com/topic/33384-file_exists-problem/#findComment-156029 Share on other sites More sharing options...
kenrbnsn Posted January 8, 2007 Share Posted January 8, 2007 Why are you looping through the directory. Just check to see if the file exists, if it doesn't return that number and use it.[code]<?php$path_thumbs = 'thmbs';$nextnum = check_for($path_thumbs);if ($nextnum === false) echo "no more files available";else { echo $nextnum; // for testing////// do work////}function check_for($path) { $ret = false; for ($i=1;$i<13;$i++) if (!file_exists($path . '/' . $i . '.jpg')) return($i); return($ret);}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/33384-file_exists-problem/#findComment-156042 Share on other sites More sharing options...
PerHaPs Posted January 9, 2007 Author Share Posted January 9, 2007 That is neat. Thanks. I tried looping because not looping didn't work. Seems main problem was path to the directory was not correct.Maybe this should be a new post?During test, if I have say 3 images in the dir, and delete the second one with the following:[code]if($delimg==1){$todelete=$path_thumbs.'/'.$delname;$opendir=opendir($path_thumbs);if(file_exists($todelete))unlink($todelete);$delimg=0;}[/code]where $delimg=1 and $delname=2.jpg are in a query string attached to a delete link,the file is successfully deleted.However, when I upload a new one, which gets the name 2.jpg, the ORIGINAL 2.jpg image appears.How is that possible? Quote Link to comment https://forums.phpfreaks.com/topic/33384-file_exists-problem/#findComment-156225 Share on other sites More sharing options...
Jessica Posted January 9, 2007 Share Posted January 9, 2007 Your browser is caching the image. Quote Link to comment https://forums.phpfreaks.com/topic/33384-file_exists-problem/#findComment-156249 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.