为什么函数finfo_file在codeigniter中显示错误的mime类型';s的上传类


Why does function finfo_file show the wrong mime type in codeigniter's upload class?

我注意到,在尝试上传常规txt文档时,出现了一个"文件类型不允许"的错误。我使用以下命令验证了mime类型:

~ $ file --mime /home/user/Documents/New_Linux_Installation.txt
/home/user/Documents/New_Linux_Installation.txt: text/plain; charset=utf-8

然而,codeigniter的upload类给了我这种mime类型:text/x-lisp。知道为什么会这样吗?我使用的是PHP 5.3.8。以下是生成MIME类型的代码部分。我添加了log_message部分来显示mime类型:

protected function _file_mime_type($file)
    {
        // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
        $regexp = '/^([a-z'-]+'/[a-z0-9'-'.'+]+)(;'s.+)?$/';
        /* Fileinfo extension - most reliable method
         *
         * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
         * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
         */
        if (function_exists('finfo_file'))
        {
            $finfo = finfo_open(FILEINFO_MIME);
            if (is_resource($finfo)) // It is possible that a FALSE value is returned, if there is no magic MIME database file found on the system
            {
                $mime = @finfo_file($finfo, $file['tmp_name']);
                finfo_close($finfo);
                log_message('error', 'mime: '.var_export($mime, TRUE), '', 'debug');
                /* According to the comments section of the PHP manual page,
                 * it is possible that this function returns an empty string
                 * for some files (e.g. if they don't exist in the magic MIME database)
                 */
                if (is_string($mime) && preg_match($regexp, $mime, $matches))
                {
                    $this->file_type = $matches[1];
                    return;
                }
            }
        }

有趣的是,原始文件上传信息确实显示了正确的mime类型。我说的是$_FILES['filename']['type']变量。在代码点火器的处理过程中,它将其更改为错误的代码。

我从来没有弄清楚它显示错误MIME类型的确切原因,但如果其他人也有同样的问题,我最终扩展了上传类,复制了_file_mime_type函数,并禁用了if (function_exists('finfo_file'))语句,以使它进入下一个获取MIME类型的方法。