Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中检查URL是否为视频文件的最佳方法?_Php_File_Video_Http Headers - Fatal编程技术网

在PHP中检查URL是否为视频文件的最佳方法?

在PHP中检查URL是否为视频文件的最佳方法?,php,file,video,http-headers,Php,File,Video,Http Headers,我试图找到一种方法(几乎)确保URL是真实的视频文件 当然,我已经检查了get_头,以检查URL是否存在以及头内容类型: 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是真实的视频文件

当然,我已经检查了get_头,以检查URL是否存在以及头内容类型:

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;
    }
 }
函数get\u http\u response\u code($theURL) { $headers=get_headers($theURL); 返回substr($headers[0],9,3); } 函数isURLExists($url) { if(intval(获取http响应代码($url))<400) { 返回true; } 返回false; } 函数isFileVideo($url) { $headers=get_headers($url); $video_exist=内爆(“,”,$headers); if(strpos($video_exist,'video')!==false) { 返回true; } 其他的 { 返回false; } } 也许我会回答我自己,但也许还有其他更健壮的解决方案(主要针对视频类型)。 我不知道这是否可行,但我可以先下载文件元数据并返回与此测试相关的文件吗

非常感谢

您可以尝试此代码

<?php
function getUrlMimeType($url) {
$buffer = file_get_contents($url);
$finfo = new finfo(FILEINFO_MIME_TYPE);
return $finfo->buffer($buffer);
}
?>
如果您想从文件内部获取
$url
的一部分,请使用

$filename = $url;
$portion=8192; // if you want upto 8192 byte to read
$handle = fopen($filename, "rb");
$contents = fread($handle, $portion);
fclose($handle);
$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);
然后您可以检查文件的mime类型。
谢谢,当然你不能确定,但是最好的做法是检查文件的第一个字节,并根据这些信息识别MIME类型


在本问答中可以找到这样一个例子:

并非所有URL都正确地报告mime类型。您最好抓取文件的前几KB,并运行它,然后在您的端执行mime确定。
get\u http\u response\u code()
看起来非常脏。我不相信子字符串是有效的http状态代码。如果在返回值上使用'intval()',则在函数遭到破坏的所有情况下,都将为0。因为0<400。。。Boom该函数将下载整个文件
$url
?对于大型视频文件,可能不需要。谢谢您的建议,但是php
stream\u get\u meta\u data
函数呢?也许我可以用它来获取我需要的元数据并检查文件类型。。。