在PHP中检测MIME类型失败 [英] Detecting a mime type fails in php

查看:95
本文介绍了在PHP中检测MIME类型失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下PHP代码显示了上传文件的mime类型.

I have the following PHP code that shows the mime type of an uploaded file.

<?php

if ($_POST) {

    var_dump($_FILES);

    $finfo = new finfo(FILEINFO_MIME_TYPE);

    var_dump($finfo->file($_FILES['file']['tmp_name']));

} else{
    ?>
    <form method="POST" enctype="multipart/form-data"><input name="file" type="file"><input name="submit" value="send" type="submit"/></form>
    <?php
}

使用此脚本上传somefile.csv的结果如下.

The result of uploading somefile.csv with this script is as follows.

array (size=1)
    'file' =>
    array (size=5)
        'name' => string 'somefile.csv' (length=12)
        'type' => string 'text/csv' (length=8)
        'tmp_name' => string '/tmp/phpKiwqtu' (length=14)
        'error' => int 0
        'size' => int 3561
string 'text/x-fortran' (length=14)

因此,MIME类型当然应该是text/csv.但是我使用的框架(Symfony 1.4)将这种方法与fileinfo一起使用.

So of course the mime type should be text/csv. But the framework I use (Symfony 1.4) uses the method with fileinfo.

我还进行了进一步测试,似乎命令(在Ubuntu上)file --mime-type somefile.csv返回somefile.csv: text/x-fortran,命令mimetype somefile.csv返回somefile.csv: text/csv. somefile.csv是使用MSOffice创建的(我不知道这是否重要). 显然mimetype使用了一些很棒的mime数据库( http://freedesktop.org/wiki/Software /shared-mime-info ),而file则没有.

Also I tested a little further it seems that the command (on Ubuntu) file --mime-type somefile.csv returns somefile.csv: text/x-fortran and the command mimetype somefile.csv returns somefile.csv: text/csv. somefile.csv is created with MSOffice (I don't know if this matters). Apparently mimetype uses some awesome mime database (http://freedesktop.org/wiki/Software/shared-mime-info), while file does not.

  1. PHP是否使用filemimetype还是都不使用?
  2. 此外,我不确定该怎么做;我上传的文件格式错误?我必须使用其他的mime数据库吗? PHP被窃听了吗?这是怎么回事?
  1. Does PHP use file or mimetype or neither?
  2. Further, I am not sure what to do here; is my uploaded file wrongly formatted? Do I have to use a different mime database? Is PHP bugged? What is going on here?

edit:

将其检测为fortran程序的原因是somefile.csv仅包含以下内容:

The reason why it is detected as a fortran program is because somefile.csv contains only the following:

somecolumn;
C F;

我认为CSV文件的上述内容有效吗?如果一个字段包含空格,则不必将该字段放在引号内,对吗?

I believe the above contents of a CSV file is valid right? If a field contains a space this field does not have to be put inside quotes, right?

推荐答案

我在这里没有Unix框可以检查真实的魔术"文件(用于猜测MIME类型的签名数据库),但是Google进行了快速搜索这个:

I don't have a Unix box here to inspect a real "magic" file (the signatures database used to guess mime types) but a quick Google search revealed this:

# $File: fortran,v 1.6 2009/09/19 16:28:09 christos Exp $
# FORTRAN source
0       regex/100       \^[Cc][\ \t]    FORTRAN program
!:mime  text/x-fortran

显然,它扫描文件的开头以查找以单个C字母加空格开头的行,这似乎是

Apparently, it scans the start of the file looking for lines that begin with a single C letter plus spaces, which seem to be a Fortran style comment. Thus the false positive:

somecolumn;
C F;

这篇关于在PHP中检测MIME类型失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆