joosh Posted July 24, 2009 Share Posted July 24, 2009 Hello, everything I've tried can't make this work, so here is what I want in long form: $filepath = 'img/' . $_POST['anim_name'] . '/' . $_POST['anim_name'] . '-' . $_POST['date'] . '-01.jpg'; if (file_exists($filepath)) { $filepath = 'img/' . $_POST['anim_name'] . '/' . $_POST['anim_name'] . '-' . $_POST['date'] . '-02.jpg'; if (file_exists($filepath)) { $filepath = 'img/' . $_POST['anim_name'] . '/' . $_POST['anim_name'] . '-' . $_POST['date'] . '-03.jpg'; if (file_exists($filepath)) { $filepath = 'img/' . $_POST['anim_name'] . '/' . $_POST['anim_name'] . '-' . $_POST['date'] . '-04.jpg'; if (file_exists($filepath)) { $filepath = 'img/' . $_POST['anim_name'] . '/' . $_POST['anim_name'] . '-' . $_POST['date'] . '-05.jpg'; } } } } Right before the file extension .jpg there is a sequence number (01, 02, etc, must have leading 0). I want it to increment the sequence number when file_exists() returns false and keep incrementing until it gets to an acceptable file name. I haven't figured out how to make: while (!file_exists($filename)) work just yet. Can someone help me out with this? Thanks a lot, Josh Quote Link to comment https://forums.phpfreaks.com/topic/167223-solved-help-with-while-loop-and-file_exists/ Share on other sites More sharing options...
Philip Posted July 24, 2009 Share Posted July 24, 2009 Use a for loop. <?php // define a max define('MAX_TRIES', 99); // loop until we find one or we hit the max for($i=0, $found=false;$i<MAX_TRIES;$i++) { // format the file path $filepath = sprintf('img/%s/%s/%s-%02d.jpg', $_POST['anim_name'],$_POST['anim_name'],$_POST['date'],$int); // Check to see if it exists if(file_exists($filepath)) { // tell us it did, and set the flag to true echo 'File exists!'; $found = true; break; } } // check the glad to see if nothing was found if(!$found) { echo 'nothing was found'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167223-solved-help-with-while-loop-and-file_exists/#findComment-881693 Share on other sites More sharing options...
joosh Posted July 24, 2009 Author Share Posted July 24, 2009 It's close but not working. As you wrote it it returns a sequence of 00. Should start at 01. I'm sure it's really close. Quote Link to comment https://forums.phpfreaks.com/topic/167223-solved-help-with-while-loop-and-file_exists/#findComment-881697 Share on other sites More sharing options...
Philip Posted July 24, 2009 Share Posted July 24, 2009 Then change the initilization of $i to 1 instead of 0. Quote Link to comment https://forums.phpfreaks.com/topic/167223-solved-help-with-while-loop-and-file_exists/#findComment-881698 Share on other sites More sharing options...
joosh Posted July 24, 2009 Author Share Posted July 24, 2009 Still getting same outcome. I'm testing using the same filenames with 01 02 and 03. So i'm hoping it will return the 04. In sprintf(), $int doesn't appear anywhere else, is it undefined? Quote Link to comment https://forums.phpfreaks.com/topic/167223-solved-help-with-while-loop-and-file_exists/#findComment-881701 Share on other sites More sharing options...
Philip Posted July 24, 2009 Share Posted July 24, 2009 I'm sorry, that should be $i, not $int. Quote Link to comment https://forums.phpfreaks.com/topic/167223-solved-help-with-while-loop-and-file_exists/#findComment-881702 Share on other sites More sharing options...
joosh Posted July 24, 2009 Author Share Posted July 24, 2009 Works perfectly! Thanks for the quick solution! I modified it down to this: $max = 99; for($i=1,$found=false;$i<$max;$i++) { $filepath = sprintf('img/%s/%s-%s-%02d.jpg', $_POST['anim_name'],$_POST['anim_name'],$_POST['date'],$i); if(!file_exists($filepath)) { $found = true; break; } } In the file_exists statement I had to put the leading ! to make it work. It was stopping at 01. Thanks a lot! I just stumbled upon sprintf() a minute ago but didn't make a whole lot of sense to me. Quote Link to comment https://forums.phpfreaks.com/topic/167223-solved-help-with-while-loop-and-file_exists/#findComment-881711 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.