douan1893 2012-01-08 17:01
浏览 97
已采纳

检测到相同文件的不同文件mime类型

I'm uploading some files. Below is part of the script for the upload (I'm using codes from https://github.com/blueimp/jQuery-File-Upload/blob/master/php/index.php)

$upload = isset($_FILES[$this->options['param_name']]) ?
                $_FILES[$this->options['param_name']] : null;
$info = array();
if ($upload && is_array($upload['tmp_name'])) {
    foreach ($upload['tmp_name'] as $index => $value) {
    $info[] = $this->handle_file_upload(
    $upload['tmp_name'][$index],
            isset($_SERVER['HTTP_X_FILE_NAME']) ?
                  $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index],
            isset($_SERVER['HTTP_X_FILE_SIZE']) ?
                  $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'][$index],
            isset($_SERVER['HTTP_X_FILE_TYPE']) ?
                  $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'][$index],
                   $upload['error'][$index]
     );
}

When checked, the mime type through $_SERVER['HTTP_X_FILE_TYPE'] or $upload['type'][$index] returns application/vnd.openxmlformats-officedocument.presentationml.presentation, application/vnd.openxmlformats-officedocument.wordprocessingml.document for pptx, docx files respectively.

Now after completing the upload, I'm trying to display the files and I'm using finfo_file() to get the mime type.

$mime_type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file_path);
echo $mime_type;

For images files (like png, jpeg) & pdf, the type returns as expected but for the pptx and docx files, the mime type returned is application/zip.

Why is it not returning the same as while in uploading? Is there something wrong with my code or should I expect this? How do I decide which file type it is then?

  • 写回答

1条回答 默认 最新

  • doupaoshu8334 2012-01-08 17:39
    关注

    The same file can have different and mulitple mime-types, that is totally normal.

    Additionally the mime-type is only meta-information next to the file itself. Theoretically you can give any file any mime-type. That would not be very useful, but it works. It's just a concept.

    The finfo library will try to obtain the mime-type of a file "magically" by looking into the file trying to identify the format. Then it will return the mime-type according to it's database.

    Why is it not returning the same as while in uploading?

    The mime-type within the request is given by the HTTP client. It might guess as well, but often it takes the value from information the underlying operating system is giving for that file.

    As you can see with your issue that the more common the file-type is, the better it will match (the images).

    However as pptx and docx files are actually zip-files, the finfo library will identify those as application/zip because the headers of those files (magic numbers) show that it is technically a zip file.

    Is there something wrong with my code or should I expect this?

    You should not expect that the mime-type of finfo matches the request header mime-type. Those are two different things.

    How do I decide which file type it is then?

    That depends. You can decide to trust the http header, you can decide to trust finfo, you can decide to compare the file extensions as well and a combination of all three.

    Additionally you can decide to even add more. This entirely depends on what you do with the uploaded file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?