DanielBP Posted September 19, 2011 Share Posted September 19, 2011 <table border="1"> <tr> <th>ID</th> <th>Image</th> </tr> <?php for($i=0; $i<=100; $i++) { $url = 'http://localhost/images/icon_'.$i.'_64x64.jpg'; echo "<tr><td>".$i."</td><td><img src='".$url."' /></td></tr>"; } ?> </table> So after testing this script, I get empty cells in the table if the image doesn't exist. Can someone show me how to make it display only the images that exists in that folder ? Quote Link to comment https://forums.phpfreaks.com/topic/247472-image-check/ Share on other sites More sharing options...
xyph Posted September 20, 2011 Share Posted September 20, 2011 Perhaps using an if/then statement combined with file_exists() http://php.net/manual/en/function.file-exists.php Quote Link to comment https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270832 Share on other sites More sharing options...
QuickOldCar Posted September 20, 2011 Share Posted September 20, 2011 <table border="1"> <tr> <th>ID</th> <th>Image</th> </tr> <?php for($i=0; $i<=100; $i++) { $url = 'http://localhost/images/icon_'.$i.'_64x64.jpg'; if (file_exists($url)) { echo "<tr><td>".$i."</td><td><img src='".$url."' /></td></tr>"; } } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270834 Share on other sites More sharing options...
PFMaBiSmAd Posted September 20, 2011 Share Posted September 20, 2011 how to make it display only the images that exists in that folder ? Use glob to get a list of the images into an array and then simply iterate over the elements of that array and output the links/<img > tags for each image. Quote Link to comment https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270835 Share on other sites More sharing options...
DanielBP Posted September 20, 2011 Author Share Posted September 20, 2011 @QuickOldCar already tried that, it doesn't work Quote Link to comment https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270837 Share on other sites More sharing options...
xyph Posted September 20, 2011 Share Posted September 20, 2011 Use glob to get a list of the images into an array and then simply iterate over the elements of that array and output the links/<img > tags for each image. This is the best solution if your folder ONLY contains the images you want to echo. If it contains more, use file_exists It would be more efficient to create a folder just for those images and use PFMaBiSmAd's solution. Quote Link to comment https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270847 Share on other sites More sharing options...
ninjeh Posted September 20, 2011 Share Posted September 20, 2011 @QuickOldCar already tried that, it doesn't work You definitely would want a local solution instead of having to make 100 requests per page unless this is for some script that is used as a cron once in a while etc. You could use curl to grab the http header of the resource. <?php function get_headers($url_to_check) { $ch = curl_init($url_to_check); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_NOBODY, TRUE); curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); } ?> <table border="1"> <tr> <th>ID</th> <th>Image</th> </tr> <?php for($i=0; $i<=100; $i++) { $url = 'http://localhost/images/icon_'.$i.'_64x64.jpg'; if (get_headers($url) == 200) { echo "<tr><td>".$i."</td><td><img src='".$url."' /></td></tr>"; } } ?> </table> Alternatively, if it is really localhost, reference the image through the file structure instead of using a URL. Then use is_file as it would be much quicker than making the aforementioned http requests. Quote Link to comment https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270858 Share on other sites More sharing options...
ninjeh Posted September 20, 2011 Share Posted September 20, 2011 Oops, forgot to add return $httpcode; to the last line of the function. Quote Link to comment https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270861 Share on other sites More sharing options...
codefossa Posted September 20, 2011 Share Posted September 20, 2011 $url = 'http://blogs.pcworld.com/staffblog/archives/Seth%20MacFarlane-Family-Guy.jpg'; if (@getImageSize($url)) { echo "<img src='$url' />"; } else { echo 'not an image'; } Quote Link to comment https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270865 Share on other sites More sharing options...
PFMaBiSmAd Posted September 20, 2011 Share Posted September 20, 2011 Or simply - <?php $files = glob('images/icon_*_64x64.jpg'); foreach($files as $file){ $url = "http://localhost/$file"; echo "<tr><td><img src='".$url."' /></td></tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270869 Share on other sites More sharing options...
DanielBP Posted September 20, 2011 Author Share Posted September 20, 2011 @PFMaBiSmAd - It doesn't work, it shows only the ID/Image cells, nothing more Found a solution: for($i=0; $i<=100; $i++) { $url = 'http://localhost/images/icon_'.$i.'_64x64.jpg'; if (@fclose(@fopen($url, "r"))) { echo "<tr><td>".$i."</td><td><img src='".$url."' /></td></tr>"; } } The only problem is, I have ~70-80 icons in that folder and it takes forever to see the result. But it does skip the files that don't exist. Quote Link to comment https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270950 Share on other sites More sharing options...
PFMaBiSmAd Posted September 21, 2011 Share Posted September 21, 2011 file_exists expects the file system path/name to the file. It only works with a URL using a couple of protocols and the http protocol is NOT one that it works with. You would need to use file_exists on the path/name to the file before you form a URL from that path/name. If the code I suggested actually formed a table with a row for each image but didn't display the images, then the actual path to where the images are at and where your script is running at it isn't what you implied in the $url value you showed in the initial post. Quote Link to comment https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1271377 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.