Viewed   288 times

I've used mime_content_type() and File info but i never successed. i want to use now cURL with PHP and get the headers of the file which is hosted on another domain then extract & determine if the type is MP3 or not. ( i think the mime type of MP3 is audio/mpeg )

Briefly, i know that but i don't know how to apply it :)




PHP curl_getinfo()

  # the request
  $ch = curl_init('');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  # get the content type
  echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

  # output
  text/html; charset=ISO-8859-1


curl -I


HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
Date: Fri, 09 Apr 2010 20:35:12 GMT
Expires: Sun, 09 May 2010 20:35:12 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
Monday, December 19, 2022

If your using Apache .htaccess can be used to map mime types.

moreinfo Here is IANA's List of MIME TYPES

Monday, November 14, 2022

Yes, you can get it like this.

$file_info = new finfo(FILEINFO_MIME_TYPE);
$mime_type = $file_info->buffer(file_get_contents($image_url));
echo $mime_type;
Friday, August 12, 2022

I found that by using this instead, I get the correct mime type:

$finfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->file($_FILES['uploadName']['tmp_name'][$key]);

And as Martin mentioned in a comment above:

You should not grab the MIME type from the data given in $_FILE as this is extremely flaky and up for interpretation, as you are experiencing. Instead, do a new analysis of the uploaded temporary file, Use finfo() or similar.

Saturday, September 10, 2022

Using the finfo_file function from the FileInfo extension (enabled by default in PHP 5.3).

From the example in the documentation

$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, $filename);

In PHP versions prior to 5.3 the pecl extension can be installed

However in this case it requires the magic_open (libmagic) library

The alternative is to use the deprecated function mime_content_type($filename)

Which relies on the mime.magic file

Friday, August 19, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :