3

I'm trying to find a way to be (almost) sure that an URL is real video file.

I've of course check get_headers to check if URL exist and header content type :

function get_http_response_code($theURL)
{
    $headers = get_headers($theURL);
    return substr($headers[0], 9, 3);
}


function isURLExists($url)
{
    if(intval(get_http_response_code($url)) < 400)
    {
        return true;
    }

    return false;
 }

function isFileVideo($url)
{
    $headers = get_headers( $url );

    $video_exist = implode(',',$headers);

    if (strpos($video_exist, 'video') !== false) 
    {
        return true;
    }
    else
    {
        return false;
    }
 }

Maybe i answer to myself, but maybe there are other more robust solution ( for video type mainly) . Don't know if it's possible, but could i just download the file metadatas first and return the file related to this test ?

Thanks a lot !

angelcool.net
  • 2,505
  • 1
  • 24
  • 26
Polykrom
  • 45
  • 1
  • 5
  • http://php.net/manual/en/function.mime-content-type.php – I wrestled a bear once. Feb 29 '16 at 18:25
  • 1
    not all urls report mime-types properly. you'd be better off grabbing the first few kbytes of the file and running it through [finfo](http://php.net/finfo) and do the mime determination on your end. – Marc B Feb 29 '16 at 18:25
  • `get_http_response_code()` looks really dirty. I won't rely on the substring being a valid http status code. Using ` intval()` on the return value will give you 0 in all cases where the function went havoc. And since 0 < 400 ... BOOM – maxhb Feb 29 '16 at 18:34

2 Answers2

0

You can try this code,

<?php
function getUrlMimeType($url) {
$buffer = file_get_contents($url);
$finfo = new finfo(FILEINFO_MIME_TYPE);
return $finfo->buffer($buffer);
}
?>

You need to enable the extension on your PHP.ini

php_fileinfo.dll

If you want to download some portion of file use,

$filename = $url;
$portion=8192; // if you want upto 8192 byte to read
$handle = fopen($filename, "rb");
$contents = fread($handle, $portion);
fclose($handle);

If you want to take some portion of $url from inside file use,

$filename = $url;
$from=10000; // if you want to read file from 1000 byte
$to=9999; //if you want to read up to 999 9byte
$handle = fopen($filename, "rb");
$skip= fread($handle, $from);
$contents = fread($handle, $to);
fclose($handle);

Then you can cheque mime type of file. thanks

Masum Nishat
  • 167
  • 3
  • 15
  • That function would download the whole file `$url`? Probably not wanted in case of large video files. – maxhb Feb 29 '16 at 18:37
  • thanks for suggestions, but what about php `stream_get_meta_data` function ? Maybe i could use it to get only the meta data i need and check the file type... – Polykrom Feb 29 '16 at 20:16
0

Of course you can't be sure, but the best practice is to check the first bytes of the file and identify the MIME type based on this information.

An example of it as to find in this Q & A: https://stackoverflow.com/a/8225754/2797243

Community
  • 1
  • 1
Tino Rüb
  • 799
  • 2
  • 13
  • 27