【发布时间】:2016-02-29 18:15:25
【问题描述】:
我正在尝试找到一种方法来(几乎)确定 URL 是真实的视频文件。
我当然检查了 get_headers 以检查 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;
}
}
也许我会回答自己,但也许还有其他更强大的解决方案(主要针对视频类型)。 不知道是否可以,但我可以先下载文件元数据并返回与此测试相关的文件吗?
非常感谢!
【问题讨论】:
-
并非所有网址都正确报告 mime 类型。你最好抓住文件的前几 KB 并通过 finfo 运行它,然后在你的最后进行 mime 确定。
-
get_http_response_code()看起来很脏。我不会依赖子字符串作为有效的 http 状态代码。在函数发生严重破坏的所有情况下,在返回值上使用 `intval()` 将为您提供 0。而且由于 0
标签: php file video http-headers