简体   繁体   English

php中如何判断一个文件是不是视频类型?

[英]How to check a file is video type or not in php?

I have following script:我有以下脚本:

function listFolderFiles($dir){
    $ffs = scandir($dir);
    echo '<ol>';
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            echo '<li>'.$ff;
            if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
            echo '</li>';
        }
    }
    echo '</ol>';
}
listFolderFiles('upload');

My question is I want to detect a file is video(mp4 or mov)type, How i can detect $ff is a video type or not?我的问题是我想检测一个文件是视频(mp4 或 mov)类型,我如何检测 $ff 是否是视频类型?

    // Type contains video/mp4,video/avi,video/mpeg,video/mpg etc
    if(preg_match('/video\/*/',$_FILES['add_image_image']['type'])): 
        echo "THIS IS A VIDEO FILE";
    else:
          echo "NOT A VIDEO FILE";           
    endif;
if(end(explode(".",$ff)) =="mp4")
{
echo "its an mp4 movie";
}

There you go, for case insensitive version of the extension好了,对于不区分大小写的扩展版本

<?php
$ff="abc.MP4";
if(strtolower(end(explode(".",$ff))) =="mp4")
{
echo "its an mp4 movie";
}
?>

Use mime_content_type mime_content_type php.net使用mime_content_type mime_content_type php.net

if (mime_content_type($dir.'/'.$ff)=='video/mp4')
    echo "its mp4";

I use this code:我使用这个代码:

$mimeType = mime_content_type(public_path('uploads/' . $file));
$fileType = explode('/', $mimeType)[0]; // video|image

if ($fileType === 'video') {
    // do something
}

Please use a tool like file .请使用类似file的工具。 This answer is security aware, and a general response to uploaded file types.此答案具有安全意识,并且是对上传文件类型的一般响应。 The second advantage to using file is it will tell you more detail about the format used.使用file的第二个优点是它会告诉您有关所用格式的更多详细信息。 There are alot of combinations of formats that may be legitimately stored inside a '*.mpg' file.有很多格式组合可以合法地存储在“*.mpg”文件中。 You may not be able to deal with all of them.您可能无法处理所有这些问题。

I did a more detailed websearch, there is a list of duplicate text articles, but no reliable solutions posted.我做了一个更详细的网络搜索,有一个重复文本文章的列表,但没有发布可靠的解决方案。 There is a "magic bytes" detector in the form of fileinfo .有一个fileinfo形式的“魔术字节”检测器。 This is compiled into most recent versions of PHP (its a standard extension).这被编译成最新版本的 PHP(它是一个标准扩展)。

NB: mime_content_type() is deprecated.注意:不推荐使用 mime_content_type()。 Again, if you need to, try fileinfo同样,如果需要,请尝试fileinfo

You can achive with preg_match您可以使用preg_match实现

if(preg_match('/^.*\.(mp4|mov)$/i', $filename)) {
    echo $filename;
}

You can append another video ext like: (mp4|mov|mpg|mpeg|wmv|mkv)您可以附加另一个视频扩展,如: (mp4|mov|mpg|mpeg|wmv|mkv)

You can also try this.你也可以试试这个。 Should be fairly accurate and performant应该相当准确和高效

<?php 
 function isVideo($file) {
   return is_file($file) && (0 === strpos(mime_content_type($file), 'video/'));
 }
?>

I recently use this to get the file type ie image or video:我最近用它来获取文件类型,即图像或视频:

explode('/', $request->article_thumbnail->getMimeType())[0]

Get the mime type of the uploading file and check the type like below,获取上传文件的 MIME 类型并检查类型如下,

$mime = $file->getMimeType;
$videoJS = array('video/mp4','video/ogg','video/webm');

if(array_search($mime, $videoJS) !== false) {
    //do upload
}
$fileType = exec( 'file --mime-type '.escapeshellarg($filePath)); //e.g. output -> /tmp/somefile.mov: video/quicktime
$fileType = substr($fileType, strpos($fileType, ": ") + 2); //strip away file path -> video/quicktime
$fileType = substr($fileType, 0,strpos($fileType, "/")); //strip away whatever video type -> video
if ($fileType == 'video') $fileType = 'im a video!';

This code uses unix 'file' command with the option --mime-type to minimize the output for easier parsing.此代码使用带有选项 --mime-type 的 unix 'file' 命令来最小化输出以便于解析。 Not sure if there is a better way of parsing the output.不确定是否有更好的方法来解析输出。

Also make sure you have read permission of the file you are checking.还要确保您对正在检查的文件具有读取权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM