我注意到在尝试上传常规 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 的处理过程中,它会将其更改为错误的。