Since you stated in the comments that the URL to the images is localhost/downloads/vf/images
, I will assume that with usage of $_SERVER['DOCUMENT_ROOT']
takes care of the folders outside public (which could be something like C:\users\chirag\htdocs\
, but that $_SERVER
variable should take care of that. If that's not the case, find the full path and use that instead - as file_exists()
requires the system path, not the public path/URL. This also assumes that you use a relative path for the $images
variable, which means that you have $images = "/downloads/vf/images/";
and not $images = "localhost/downloads/vf/images/";
.
With that assumption, we can now use file_exists()
- because that takes the system path, not the URL.
<?php
$images = "/downloads/vf/images/";
for ($i = 1; $i <= 9; $i++):
if (!file_exists($_SERVER['DOCUMENT_ROOT'].$images.$i.".png"))
continue; // Skip this iteration if it can't find the file
?>
<div class="col-md-2">
<div class="thumbnail">
<a href="<?php echo $details . $i;?>.php">
<img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
</a>
</div>
</div>
<?php endfor; ?>