Sam Granger Posted October 7, 2006 Share Posted October 7, 2006 I have the following sourcecode:<?php$ad = $_GET['a'];$filename = "./uploads/$ad.jpg";if (file_exists($filename)) { echo "<img src='$filename' alt='' />";}?>Currently when i go to filename.php?a=1 it displays 1.jpg if it exists. However, I want it to check if 1_1.jpg, 1_2.jpg, 1_3.jpg etc... exists. Lets say those 3 files exists but 1_4 doesnt, then it has to stop checking (stop the looping). How can I do this? Quote Link to comment Share on other sites More sharing options...
Orio Posted October 7, 2006 Share Posted October 7, 2006 [code]<?php$ad = $_GET['a'];$i=1;$continue=TRUE;while($continue){ $filename = "./uploads/".$ad."_".$i.".jpg"; if (file_exists($filename)) { echo "<img src='$filename' alt='' />"; } else { $continue=FALSE; }$i++;}?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
Sam Granger Posted October 7, 2006 Author Share Posted October 7, 2006 Thanks! Works like a dream! :) Quote Link to comment Share on other sites More sharing options...
alpine Posted October 7, 2006 Share Posted October 7, 2006 Or use foreach[code]<?php$files = array("one.jpg","two.jpg","whatever.jpg");foreach($files as $file){ if(file_exists($file)) echo "$file does exist <br />"; else break;}?>[/code] 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.