3

Does any one know how to check media types of audio and video from external urls.

function get_url_mime_type($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_exec($ch);
    return curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
}
Raptor
  • 53,206
  • 45
  • 230
  • 366

1 Answers1

-1

To get file's MIME, use the following codes:

<?php
$finfo = new finfo(FILEINFO_MIME, "/usr/share/misc/magic"); // return mime type a.k.a. mimetype extension

/* get mime-type for a specific file */
$filename = "/usr/local/something.txt";
echo $finfo->file($filename);
?>

To get the extension of the file:

$tokens = explode('.', $filename);
$extension = $tokens[count($tokens)-1];

To get the MIME type of external file, either you can use cURL HEAD request to get the header ( may not be accurate , as web server may not response with correct answer ), or use getID3.

This question also asks the similar thing. Take a look of it.

Reference: finfo_open()

Community
  • 1
  • 1
Raptor
  • 53,206
  • 45
  • 230
  • 366
  • if i use curl_getinfo() some times for audio urls i got media type "text/html" that's the problem. –  Apr 23 '13 at 07:29
  • Do you use HEAD request as my referenced question ? – Raptor Apr 23 '13 at 08:30
  • got it ! but you are not requesting a file. You are requesting a page. Your URL should look like http://www.example.com/example.mp3 – Raptor Apr 23 '13 at 09:01