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?
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.
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.
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…
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.
I was trying to figure out how to put it all together. This is what I originally came up with…
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…
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.
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.
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.“.*”);