Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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].*

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

Another idea may be something like
<?php $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 <?php 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.
HTH

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |