Allowing Variable Image Extensions (Glob?)

Imagine a folder filled with images of maps of each of the 50 U.S. states. Each image is named with a state’s ID - e.g. ca.jpg for California.

If I visit MySite/world/california, the following script correctly selects the California map and later displays it in the proper location:

$TopImg = '<img src="/images/sections/world/top/usa/'.$MyID.'.jpg">

echo $TopImg;

The problem is that a few images end with the .png or .gif extension. So I’d like to modify my script so it accepts any image with the proper name, regardless of extension.

The logical solution appears to be PHP’s glob function. It looks ridiculously easy to figure out, and I’ve found several online examples, yet I can’t get it to work. At best, it displays “array.”

Can someone show me how to incorporate glob into the example above? Or is there an alternative solution that would work better?

Thanks.

is there an alternative solution that would work better?

Sure.

Rename your images to make them have a uniform extension.

1 Like

Well, that’s correct - glob() returns an array containing the name(s) of file(s) that match your specification. You could just do something like

$names = glob("ca.*");
$TopImg = "all/that/stuff/" . $names[0];
echo $TopImg;

But if they’re all your images, I’d go with establishing a standard so they’re all the same. Dealing with different image formats and names is the kind of thing you have to do when you let end users upload their stuff, not for your own things.

Yes, I suppose I could change all my .gifs and .pngs to .jpgs, even if it increases the file size a bit. Thanks.

1 Like

No need to change the format. You can change only the extension.

1 Like

If I change the extension, doesn’t that automatically change the “format”? For example, if I have a .png with a transparent background, changing it to a .jpg will kill the transparency, won’t it? I guess I’ll have to experiment with a few images.

1 Like

It still isn’t working for me. print_r($names) displays “Array ().” I get the same results when I replace the asterisk with the image extensions in curly braces…

$names = glob("ca.{jpg,png,gif}");
print_r($names);

$TopImg = "/images/sections/world/top/flower/usa/".$names[0];
$TopImg2 = '<img src="'.$TopImg.'">';
echo $TopImg;

Oops, I forgot the glob brace. I also added the full path. Same results.

$names = glob("/images/sections/world/top/flower/usa/ca.{jpg,png,gif}", GLOB_BRACE);
print_r($names);
1 Like

glob takes a pattern (as in Regular Expression, kinda)

ca.(jpg,png,gif)

will match files with names like

ca.jpg,png,gif

Something tells me you don’t have many files named like that.

In this particular example, I had just one - ca.jpg. I made a copy and renamed it ca.png but still no luck. But maybe I’m just making a simple mistake.

On another note, I’m wondering if glob is the best solution. I didn’t realize that it apparently requires multiple images (an array). I’m only going to have ONE image with a particular name, most of them jpgs. I just want a script that displays the rare exceptions, like images with .png extensions.

I’ll have to go back and reread this entire thread. I can always just make a PHP switch and code in the exceptions manually. Thanks.

As far as I can tell, droopsnoot gave you an extremely simple answer several posts back. Did you try that?

That’s what I’m working on right now. :wink: It didn’t work for me, so I started experimenting with various modifications.

What’s in $names[0] after that call to glob()? Isn’t it the file name, whether it’s ca.jpg or ca.png or ca.gif?

1 Like

The pattern
ca.*
should match files like
ca.jpg
ca.png
ca.gif
ca.txt

i.e.
any file beginning with “ca”

if it isn’t working are you sure the

all/that/stuff/
is/the/correct/path/
?

I was trying to figure out how to put it all together. This is what I originally came up with…

  1. Define names as the name of an image matching a webpage’s ID. So if I’m visiting MySite/california, the page ID is ca, so the image is probably named ca.jpg. But if it’s ca.png instead, then glob will hopefully fix that problem…
$names = glob("'.$IDParent.'.*");
  1. Map out the entire image path…
$TopImg = "/images/sections/world/top/flower/usa/".$names[0];
  1. Put it all together…
$TopImg2 = '<img src="'.$TopImg.'">';
echo $TopImg2;

It sounds like I have the correct script and syntax, so it must be a case of operator error. I’ll continue playing with it and see if I can figure out what I’m doing wrong. Thanks.

1 Like

Nope. Changing the file extension is like changing the filename. Nothing in the image is altered except the name. That’s why trusting file extensions during upload is not safe at all. You can basically create a text file, put in malicious code in it, change the file extension to .jpg, .png, .gif and if someone’s upload system is a legacy one that is copied&pasted from a tutorial from 1990, people can basically hack your website. They just need to do something like test.php.jpg and the upload system will allow the upload because it’s a .jpg extension, but PHP might interpret the file as an executable PHP file which is a security flaw.

Wow, that’s good to know. Thx.

$names = glob($IDParent.“.*”);

would seem much simpler!

OK, I changed it. But it’s going to be an empty array without the full path, right? So I guess I have to replace $IDParent with the full path?

I was just copying your code from above, but yes if the php file is not in the same place as the various images, then you do need the path in there. Something like:
$names = glob($path.$IDParent.“.*”);