Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php upload not recoginsing .flv and f4v extensions

I have a page for uploading videos, and I want the file types to be mp4 flash and webm files. all the mp4 extension files and webm files work fine but the flash files (.flv and .f4v) are not recognized and therefore php returns the value 'application/octet-stream' which I believe is the default MIME for any files which aren't recognized. is there a better method than this code that will recognize the flash files for there true MIMES (video/x-flv and video/x-f4v) respectively?

$type   = $_FILES["uploaded"]["type"];
$allowedTypes  = ["video/mp4","video/quicktime","video/x-flv","video/x- f4v",  
                 "video/webm"];
$isRefusedType = !in_array($type, $allowedTypes);


if ($isRefusedType)
 {
   $message="not an accepted file type";
 }
    else $message="OK";
like image 477
user1559811 Avatar asked Oct 06 '22 13:10

user1559811


2 Answers

Perhaps you can try this code:

<?php
    $finfo = finfo_open(FILEINFO_MIME_TYPE); // mimetype
    $type  = finfo_file($finfo, $filename);
    finfo_close($finfo);
?>

As described here, you will need PHP >= 5.3.0, is it a problem?

Another idea could be to check the data signature (see there):

<?php
    $s = file_get_contents($_FILES["uploaded"]["tmp_name"]);
    if ( substr($s,0,3) == 'FLV' ) { /* It's good! */ } 
?>
like image 114
Tiger-222 Avatar answered Oct 10 '22 03:10

Tiger-222


Watching at the description of $_FILES['userfile']['type'] in the php documentation we can see:

The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.

So the Tiger-222 option seems to be your best chance to get the real value:

<?php
    $finfo = finfo_open();
    $fileinfo = finfo_file($finfo, $file, FILEINFO_MIME);
    finfo_close($finfo);
?>
like image 26
darkheir Avatar answered Oct 10 '22 02:10

darkheir