I am using file_exists to determine whether certain image files are in the specified directory, and if they are, displaying them within an image rotator. It works fine but the code seems very long winded for what it achieves. Is there a better way?
At the moment I have:
<?php
if (file_exists($reg_photo_1_f)) {
echo "<li>";
echo "<a href=". $reg_photo_1_f .">"."</a>";
echo "</li>";
}
if (file_exists($reg_photo_2_f)) {
echo "<li>";
echo "<a href=". $reg_photo_2_f .">"."</a>";
echo "</li>";
}
if (file_exists($reg_photo_3_f)) {
echo "<li>";
echo "<a href=". $reg_photo_3_f .">"."</a>";
echo "</li>";
}
if (file_exists($reg_photo_4_f)) {
echo "<li>";
echo "<a href=". $reg_photo_4_f .">"."</a>";
echo "</li>";
}
if (file_exists($reg_photo_5_f)) {
echo "<li>";
echo "<a href=". $reg_photo_5_f .">"."</a>";
echo "</li>";
}
?>
Where each $reg_photo_x_f is the image path. I'm hoping there is a way that I can adapt this code so that it can loop until all images are found to exist. The image names themselves are hashes generated from another page, so are obviously not consecutive.
The page creating the hashes combines a value relevant to the page in the form of a php variable with a pre-defined 'salt' so that the image name is matched almost in the same way as matching a hashed password in a database etc. For example, for $reg_photo_1_f I have this code on the same page:
$reg_photo_1 = sha1("$bh".PHOTO_REG_1);
$reg_photo_1_f = $reg_photo_root.$reg_photo_1.".jpg";
...where $bh is the variable relevant to the page the user is on and PHOTO_REG_1 is the 'salt'.
Sure there is a much better way to name the images too but this was the simplest way I could think of for it to work with dynamic pages.