Jump to content

Recommended Posts


<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 ?

Link to comment
https://forums.phpfreaks.com/topic/247472-image-check/
Share on other sites

<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>

Link to comment
https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270834
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270847
Share on other sites

@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.

 

Link to comment
https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270858
Share on other sites

@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.

Link to comment
https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1270950
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/247472-image-check/#findComment-1271377
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.