Bhumi Shah
Bhumi Shah

Reputation: 9476

How to fix "Failed identify data 0:no magic files loaded" error for MIME type in PHP?

I have tried both the options:

  1. mime_content_type function

    echo mime_content_type($img_path);
    
  2. finfo function

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    echo finfo_file($finfo, $img_path);
    finfo_close($finfo);
    

But I am getting error for both the cases

"Warning: finfo_file(): Failed identify data 0:no magic files loaded"

I am not getting what is the issue in this?

Upvotes: 3

Views: 10708

Answers (2)

Wackyedd
Wackyedd

Reputation: 91

For people wondering in the future, the original poster probably had an external URL set as the path - both the "mime_content_type" & "finfo" functions can only be ran on local paths and not external URLS.

Example:

// Local file path
echo mime_content_type("image.gif"); 

Will return: image/gif

// External file path
echo mime_content_type("http://localhost/image.gif");

Will return "Warning: mime_content_type(): Failed identify data 0:no magic files loaded"

Fallback versions and more information on usage of these functions can be found in the manual on PHP.net: mime_content_type: http://php.net/manual/en/function.mime-content-type.php

finfo functions: http://php.net/manual/en/book.fileinfo.php

Upvotes: 8

Hardik Patel
Hardik Patel

Reputation: 838

looks like $img_path is not containing any file or wrong file path.

Upvotes: 1

Related Questions