wilcard / if or statement

September 28, 2009 at 22:39:26
Specs: Windows Vista
Hi there... I don't know anything about php, so don't even know what to search for to get the right answer...

I have this piece of code
$image = 'logo'.$_GET['im'].'.jpg';

problem is, the image can either be a jpg or a gif or even a png. How can I change this line to display the logo whether it's a gif, jpg, or png? Is it possible to use some sort of a wildcard?
the name will always be the same: logo[im].*

See More: wilcard / if or statement

Report •

October 2, 2009 at 15:01:39
Hi, not sure if there's a wild card that you can use.

One approach would be to extract the file extension, then process it to display the image. Perhaps here's one way on how to identify the file extension:

function findexts ($filename)
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;

Report •

October 3, 2009 at 02:05:22
Another idea may be something like


   $image = file_exists('logo' . $_GET['im'] . '.jpg') ?
            'logo' . $_GET['im'] . '.jpg' :
            file_exists('logo' . $_GET['im'] . '.gif') ? 
            'logo' . $_GET['im'] . '.gif' :
            file_exists('logo' . $_GET['im'] . '.png') ?
            'logo' . $_GET['im'] . '.png' : null;

   if(!is_null($image)) {
      echo "<img src='" . $image . "'>";

You might also try using glob for wildcards


   foreach(glob('path/logo' . $_GET['im'] . '.*') as $fname) {
      echo "<img src='" . $fname . "'>";

The glob would produce multiple images if more than one were found or perhaps an error if the directory was empty, so you'd want to modify it as needed.


Report •

Related Solutions

Ask Question