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 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败