Search code examples
phphttpmime-typesfile-get-contentscontent-type

How to get MIME-type of an image with file_get_contents in PHP


I need to get the MIME type of an image, but I only have the body of the image which I've got with file_get_contents. Is there a possibility to get the MIME type?


Solution

  • Yes, you can get it like this.

    $image_url = "mydomain.com/myimage.jpg";
    $file_info = new finfo(FILEINFO_MIME_TYPE);
    $mime_type = $file_info->buffer(file_get_contents($image_url));
    echo $mime_type;