5
votes

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";
2

2 Answers

2
votes

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! */ } 
?>
2
votes

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);
?>