Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image extension

Tags:

php

image

I want get uploaded image extension.

As I know, best way is getimagesize() function.

but this function's mime, returns image/jpeg when image has .jpg or also .JPEG extension.

How can get exactly extension?

like image 978
Oto Shavadze Avatar asked Feb 07 '13 08:02

Oto Shavadze


People also ask

How do I find out the extension of an image?

you can use php built-in pathinfo function. <? php $supported_image = array( 'gif', 'jpg', 'jpeg', 'png' ); $src_file_name = 'abskwlfd.

How do I use image downloader extension?

Click Image Downloader extension for Chrome saves you time by allowing you to save images with a keyboard shortcut. Simply hold the Shift key and right-click which saves you a wee bit of time. The extension works well with Google Images and Bing, but not with other websites.

How do I see image extensions in Chrome?

To add the View Image extension, visit its page in the Chrome Web Store, click Add to Chrome, and then click Add extension in the following pop-up window. The View Image button should then show up in following Google Image searches.


6 Answers

you can use image_type_to_extension function with image type returned by getimagesize:

$info = getimagesize($path);
$extension = image_type_to_extension($info[2]);
like image 70
lupatus Avatar answered Oct 22 '22 18:10

lupatus


$ext = pathinfo($filename, PATHINFO_EXTENSION);
like image 40
phpalix Avatar answered Oct 22 '22 20:10

phpalix


You can also use strrpos and substr functions to get extension of any file

$filePath="images/ajax-loader.gif";

$type=substr($filePath,strrpos($filePath,'.')+1);

echo "file type=".$type;

output: gif

if you want extension like .gif

$type=substr($filePath,strrpos($filePath,'.')+0);

output: .gif

like image 39
vasudev Avatar answered Oct 22 '22 20:10

vasudev


$image = explode(".","test.file.hhh.kkk.jpg");
echo end($image);
like image 38
Devang Rathod Avatar answered Oct 22 '22 20:10

Devang Rathod


One more way to do it:

$ext = strrchr($filename, "."); // .jpg
like image 44
dfsq Avatar answered Oct 22 '22 20:10

dfsq


$file_ext = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);

or to make it clean

$filename= $_FILES["file"]["name"];
$file_ext = pathinfo($filename,PATHINFO_EXTENSION);
like image 30
James Avatar answered Oct 22 '22 20:10

James