retrieve img witj unknown extension


I have a folder that contains images with different extensions. Like 1.jpg, 2.png, 3.gif etc
The images have different names ( 1,2,3,4,5,6 …) but their extensions are of different types.

What I try to so is to retrieve an image when I only know the image-name but not the extension.

This is not working:

Anyone who may help me???

One way to do it would be with something similar to this:


<?php //$root is the root to the directory where the images are, so I have put /images/ but yours may be different $root = "/images/"; $png = ".png"; $gif = ".gif"; $jpg = ".jpg"; //assuming the filename (for example "1") is in a variable called $filename $filename = $root.$filename; //if statements checks if a file of that name exists and sets $filename to the imagename + extension (e.g. "2.gif") if (file_exists($filename.$png)) { $filename = $filename.$png; } else if (file_exists($filename.$gif)) { $filename = $filename.$gif; } else if (file_exists($filename.$jpg)) { $filename = $filename.$jpg; } else { //echo message if none of the extentions match echo "Image Not Found"; } //echos the full filename echo ""; ?>


however there may be a simpler way, of which I do not know.

Sponsor our Newsletter | Privacy Policy | Terms of Service