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 :)

Thanks

 Answers

2

PHP curl_getinfo()

<?php
  # the request
  $ch = curl_init('http://www.google.com');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_exec($ch);

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

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

curl

curl -I http://www.google.com

output

HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
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
4

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
4

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
 
sh_khan
 
2

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
 
5

Using the finfo_file function from the FileInfo extension (enabled by default in PHP 5.3). http://www.php.net/manual/en/function.finfo-file.php

From the example in the documentation

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

In PHP versions prior to 5.3 the pecl extension can be installed http://pecl.php.net/package/Fileinfo

However in this case it requires the magic_open (libmagic) library http://sourceforge.net/projects/libmagic

The alternative is to use the deprecated function mime_content_type($filename) http://au.php.net/manual/en/function.mime-content-type.php

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 :