PHP脚本来识别是否给定的文件名字符串或url字符串是有效的视频mime类型


PHP Script to identify if given file name string or url string is of valid video mimetype

是否有可能以及如何确定给定的文件名或url字符串是否为视频的有效资源?我有一个项目,从rss源获取数据,只过滤视频。任何想法?

您需要的是curl_getinfo():

<?php
// get file headers
$ch = curl_init('http://www.test.com/data.avi');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
// get content type
$allowed_content_types = array("video/x-msvideo", "video/mp4");
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if (in_array($content_type, $allowed_content_types) {
    // your code here
}

它允许您获得准确的MIME文件类型,并检查它是否是您需要的。
如果你只是想检查文件扩展名-你可以explode url字符串点,获得最新的元素,并使用in_array来检查它是否在允许的扩展名数组:

<>之前$url = "http://test.com/data.avi";$extension = pathinfo("test.s. "d",PATHINFO_EXTENSION);$allowed_extensions = array("avi", "mp4");如果(in_array($extension, $allowed_extensions) {//你的代码在这里}

此处使用strpos

$url = 'http://example.com?index.php&file=mp4';
if (strpos($url,'mp4')) {
  echo 'true';
}