Reputation: 83
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?
Upvotes: 2
Views: 30383
Reputation: 307
I recently use this to get the file type i.e. image or video:
explode('/', $request->article_thumbnail->getMimeType())[0]
Upvotes: 1
Reputation: 109
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/'));
}
?>
Upvotes: 2
Reputation: 10717
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)
Upvotes: 2
Reputation: 1015
I use this code:
$mimeType = mime_content_type(public_path('uploads/' . $file));
$fileType = explode('/', $mimeType)[0]; // video|image
if ($fileType === 'video') {
// do something
}
Upvotes: 5
Reputation: 1251
// 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;
Upvotes: 10
Reputation: 478
$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.
Upvotes: 0
Reputation: 957
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
}
Upvotes: 0
Reputation: 722
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
Upvotes: 3
Reputation: 364
Use mime_content_type
mime_content_type php.net
if (mime_content_type($dir.'/'.$ff)=='video/mp4')
echo "its mp4";
Upvotes: 5
Reputation: 46900
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";
}
?>
Upvotes: 7