简体   繁体   中英

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?

    // 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

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 . 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. There are alot of combinations of formats that may be legitimately stored inside a '*.mpg' file. 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 . This is compiled into most recent versions of PHP (its a standard extension).

NB: mime_content_type() is deprecated. Again, if you need to, try fileinfo

You can achive with preg_match

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

You can append another video ext like: (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 = $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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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