tags:

views:

98

answers:

5

Hi,

Is there any standard function in PHP to find only extension of an image from the corresponding file path?

For example ff my image path is like '/testdir/dir2/image.gif' then the function should return 'gif'.

Thanks

+10  A: 
$ext = pathinfo('/testdir/dir2/image.gif', PATHINFO_EXTENSION); //$ext will be gif

UPDATE: I see a lot of downvotes. What's the problem with my approach? Please let me know.

fabrik
this is the way too go.
RobertPitt
I have a `hello.jpg` but i renamed it to `hello.gif` and upload it so you get the extension but not the correct type. Pekka's aproach is IMO the better one
DrColossos
The original question asks for find the extension only. No security-check or other needs.
fabrik
@DrColossos But thats what the OP requires - the extension. There is more chance of the OP requiring the same code for future use than renaming his images with wrong extensions.
zaf
I didn't say his answer is not correct, I just tried to explain a possibility of the downvotes.
DrColossos
@fabrik relax, its only one downvote, you're doing fine. +1
zaf
@zaf, thanks! :)
fabrik
@fabrik: thanks, this is the one I want to use ...
Siva
@DrColossos: thanks for your comment, anyway currently I just need to get the file extension not the file type.
Siva
A: 

Basic string functions, strrpos() and substr() can do that for you. As well as many other fancy ways.

Col. Shrapnel
This is a direct answer to a question where what the OP probably wants is a thorough file type check and not just one for the extension. By your logic, this would deserve a -1. Just sayin' ;)
Pekka
@Pekka "to find only extension of an image". Sounds rather unambiguous to me
Robus
@Robus yes. I'm just teasing @Col because he has downvoted me for direct answers to such clear requests when the clear request was, in fact, not what the OP really needed to achieve the best solution. His feedback was always justified (I value @Col's knowledge and input very much, the PHP tag needs it) but he's always being very direct about it which is why I'm being a dick in return when he makes the same mistake :) I would like to point out that the downvote this answer has received is not from me, though.
Pekka
@Pekka I hope he comes back and spanks you :P Actually I'm not sure why this was downvoted, the most upvoted answer doesn't check the actual type either just returns the extension. They're both manipulating the string rather than checking the file so not sure why someone is hating on the col.
Take it easy with Col. Shrapnel ;)
zaf
@Col. Shrapnel: I can use these functions also but I want to use a direct standard function like 'pathinfo' instead of writing my own logic using string functions.
Siva
@Siva these functions no less standard. And you can do everything using it. Programming is NOT like LEGO - putting ready made blocks together but sometimes you have to invent something of your own. And if even such an easy thing makes you trouble, then programming probably is not your vocation. Things like pathinfo is not "standard" but rather called "syntax sugar". And too much sugar will cause diabetes.
Col. Shrapnel
@Col. Shrapnel: I am not saying that those string functions are not standard. I know the power of those functions and how to use them also, but I started this thread to know about any 'DIRECT(single)' standard function to get the image extension.
Siva
A: 

As Col. Shrapnel mentioned; there's quite a few ways

$path = '/some/where/img.gif';
$path = explode('.',$path);
$path = end($path);
Robus
@Robus: thanks, this code is looking cool, but as I said above I just want to use a direct standard function like 'pathinfo'.
Siva
+8  A: 

It's usually more desirable to detect the actual image type (not by extension but by its contents). For that, use getimagesize().

Pekka
wrong link BTW ;) http://php.net/getimagesize
DrColossos
@DrC corrected, cheers!
Pekka
@Pekka: thanks, this is also a good solution to get the file extension even the correct file type. By the function name I thought this will give only image dimensions.
Siva
A: 

I would recommend you to run any uploaded/linked image(s) through a GD/ImageMagick check and re-save it to prevent any malicious codes hidden within the images. This would also allow you to save all of the images with the same extension to make things easier for you.

http://www.php.net/imagepng
http://www.php.net/imagegif
http://www.php.net/imagejpeg

Chris
This is a good suggestion, even though it's not related to the question. Doesn't deserve a downvote. However, cross-converting JPG to GIF and vice versa will never lead to good results.
Pekka
Thank you. I know it is not entirely related to the question, although I figured if he is to accept uploaded images it is better to spit this bit of information out before something bad happens. You could also check for the extension at first, look if the given result exists in an array of allowed extensions, and then save it as the exact same extension once run through the GD/ImageMagick check. Also keep a default (say PNG) if a result was not found.
Chris
@Chris: thanks, this is a good suggestion.
Siva
You're welcome. I'm just glad I could help! :)
Chris