Finfo_file on uploaded file to determine mime-type

后端 未结 3 1059
星月不相逢
星月不相逢 2020-12-31 03:51

Im trying to determine the mime-type of an uploaded file, i want to use fileinfo(), this is what ive been trying, it isnt working:

$uploadedfile = $_FILES[\'         


        
相关标签:
3条回答
  • 2020-12-31 04:18

    I know this is a bit old, but since you're using the $_FILES super global, can you use the type key of the file array (i.e. $_FILES['soup']['type']) rather than having the server check once the file is uploaded?

    0 讨论(0)
  • 2020-12-31 04:29

    I use the finfo() buffer() function as well as file_get_contents() from the php platform as below

    $finfo = new finfo(FILEINFO_MIME);
    $mimetype = $finfo->buffer(file_get_contents($filename)); #gives you mime type
    

    you need to be on php 5.3 or higher and make sure you have the finfo() extension installed. for linux extension=fileinfo. and in windows: php_fileinfo.dll

    you can have an array of accepted mime types and then check if it exists in that array

    $acceptedMime = [];
    if(in_array($mimetype, $acceptedMime, true) === true){
      #mime type is valid. Proceed!
    }
    

    Another alternative to avoid having to check mime types would be to store file uploads completely out of the document root folder.

    0 讨论(0)
  • 2020-12-31 04:30

    You should be passing the path to the finfo_file function not the filename.

    <?php 
    if (isset($_FILES['soup']['tmp_name'])) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mime = finfo_file($finfo, $_FILES['soup']['tmp_name']);
        if ($mime == 'application/msword') {
            //Its a doc format do something
        }
        finfo_close($finfo);
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题