15

I want to get the file type (eg. image/gif) by URL using PHP.I had tried

<?php
$image_path="http://fc04.deviantart.net/fs71/f/2010/227/4/6/PNG_Test_by_Destron23.png";
exif_imagetype($image_path);
?>

The above code gave me a blank page and the following code returned "3":

<?php
$image_path="http://fc04.deviantart.net/fs71/f/2010/227/4/6/PNG_Test_by_Destron23.png";
echo exif_imagetype($image_path);
?>

Where am I going wrong? Solved: using Fileinfo to fetch content type

0

7 Answers 7

18

Here is a PHP function I came up with:

/**
 * @param $image_path
 * @return string|null
 */
function get_image_mime_type(string $image_path):?string
{
    $mimes  = [
        IMAGETYPE_GIF => "image/gif",
        IMAGETYPE_JPEG => "image/jpg",
        IMAGETYPE_PNG => "image/png",
        IMAGETYPE_SWF => "image/swf",
        IMAGETYPE_PSD => "image/psd",
        IMAGETYPE_BMP => "image/bmp",
        IMAGETYPE_TIFF_II => "image/tiff",
        IMAGETYPE_TIFF_MM => "image/tiff",
        IMAGETYPE_JPC => "image/jpc",
        IMAGETYPE_JP2 => "image/jp2",
        IMAGETYPE_JPX => "image/jpx",
        IMAGETYPE_JB2 => "image/jb2",
        IMAGETYPE_SWC => "image/swc",
        IMAGETYPE_IFF => "image/iff",
        IMAGETYPE_WBMP => "image/wbmp",
        IMAGETYPE_XBM => "image/xbm",
        IMAGETYPE_ICO => "image/ico"];

    if (($image_type = exif_imagetype($image_path))
        && (array_key_exists($image_type ,$mimes)))
    {
        return $mimes[$image_type];
    }
    return NULL;
}
2
  • I get this code and works! thanks. Only copy and paste. workfull! Commented Jul 11, 2018 at 13:05
  • The question is PHP- Get file type by URL - this answer only handles image URLs.
    – Avatar
    Commented Mar 3 at 15:16
16

the best way for my understanding

if (!function_exists('getUrlMimeType')) {
    function getUrlMimeType($url)
    {
        $buffer = file_get_contents($url);
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        return $finfo->buffer($buffer);
    }
}

is to create function depend on finfo class

2
  • 2
    This should be the approved answer. It's the cleanest imo Commented Dec 2, 2020 at 22:12
  • This gets the filetype, e.g. video/quicktime for mov files.
    – Avatar
    Commented Mar 3 at 15:18
9
<?php
 $image_path="http://fc04.deviantart.net/fs71/f/2010/227/4/6/PNG_Test_by_Destron23.png";
 echo exif_imagetype($image_path);
?>

It returned 3 because png response type as maciej said.

Try this to get like this image/png:

echo mime_content_type($image_path);

Try this:

$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension   
echo finfo_file($finfo, $image_path) . "\n";
finfo_close($finfo);
6
  • When I store url in $image_path it says: Warning: mime_content_type($image_path) [function.mime-content-type]: failed to open stream: No such file or directory in /home/appfesti/public_html/y.php on line 4 Commented Sep 18, 2014 at 17:37
  • yes it is deprecated... $finfo = finfo_open(FILEINFO_MIME_TYPE);
    – Avis
    Commented Sep 18, 2014 at 17:43
  • @jGrice Can I use fileinfo with url? Commented Sep 18, 2014 at 17:48
  • got it?@AshishSrivastava
    – Avis
    Commented Sep 18, 2014 at 17:55
  • @user3923716 When I used URL i got Warning: finfo_file() [function.finfo-file]: Failed identify data 0:(null). So I decided to store the file on my server instead of querying with url :) Commented Sep 18, 2014 at 18:01
6

You are not going wrong anywhere. exif_imagetype returns the value of one of the image type constants: http://php.net/manual/en/image.constants.php

If you would like to convert this to an extension string, you could use a switch statement:

$typeString = null;
$typeInt = exif_imagetype($image_path);
switch($typeInt) {
  case IMG_GIF:
    $typeString = 'image/gif';
    break;
  case IMG_JPG:
    $typeString = 'image/jpg';
    break;
  case IMG_JPEG:
    $typeString = 'image/jpeg';
    break;
  case IMG_PNG:
    $typeString = 'image/png';
    break;
  case IMG_WBMP:
    $typeString = 'image/wbmp';
    break;
  case IMG_XPM:
    $typeString = 'image/xpm';
    break;
  default: 
    $typeString = 'unknown';
}

You may want to change the order to most to least frequently expected for better performance.

1
  • 1
    The IMG_JPG case should be removed, image/jpg is not a valid MIME type.
    – timetofly
    Commented Sep 28, 2017 at 17:02
3

In the first example, you're getting a blank page because you're not doing anything with the return value from the function call. In the second example, you're getting a valid response. See the manual page for exif_imagetype() for a list of what the values mean.

1
  • 1
    thanx for the link, we can use image_type_to_mime_type(exif_imagetype($url)) to get the 'image/xxx' mime type
    – ctf0
    Commented Jan 11, 2018 at 20:36
2

exif_imagetype returns the image type. The response, 3, indicates it is IMAGETYPE_PNG, the correct response.

2
  • isnt it possible to get output like "image/png"? Commented Sep 18, 2014 at 17:29
  • You can either hard code a switch and compare the value against it, or use finfo_file to get the MIME type (image/png)
    – Grice
    Commented Sep 18, 2014 at 17:37
1

3 is image type response for PNG image. See: http://php.net/manual/en/function.exif-imagetype.php

3
  • isnt it possible to get output like "image/png"? Commented Sep 18, 2014 at 17:28
  • then you have to write...mime_content_type @AshishSrivastava
    – Avis
    Commented Sep 18, 2014 at 17:29
  • 1
    @user3923716 I get: Warning: mime_content_type() [function.mime-content-type]: Failed identify data 0:(null) in /home/appfesti/public_html/y.php on line 4 Commented Sep 18, 2014 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.