繁体   English   中英

为什么函数finfo_file在codeigniter的上载类中显示错误的mime类型?

[英]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的上载类为我提供了这种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']变量。 它是在codeigniter的处理过程中将其更改为错误的。

我从来没有确切查明为什么它显示了错误的MIME类型,但是如果其他任何人都遇到了同样的问题,我最终扩展了上载类,复制了_file_mime_type函数,并禁用了if (function_exists('finfo_file'))语句,以便继续进行下一种获取mime类型的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM