获取上传文件的 mime 类型非常简单:
echo mime_content_type($fileatt['tmp_name']);
但是,我还想检查压缩文件中包含的文件的 mime 类型。解压缩我的文件后(遍历 zip 中的文件,i
是当前文件),我试过:
$info = pathinfo($zip->getNameIndex($i));
echo mime_content_type($info['dirname'] . "\\" . $info['basename']);
给出错误:警告:mime_content_type() [function.mime-content-type]:在 C:\Users\<user>\<website>\validation.php
中找不到文件或路径 '.\foo.pdf'在第 56 行
我意识到 dirname
对于压缩文件是相对于 zip 文件的,而不是绝对路径,所以我试过:
$a = pathinfo($fileatt['tmp_name']);
$b = $a['dirname'] . "\\" . $info['basename'];
echo mime_content_type($b);
给出错误:警告:mime_content_type() [function.mime-content-type]:在 C:\Users\<user>\<website>\validation.php
中找不到文件或路径 'C:\xampp\tmp\foo.pdf'在第 56 行
任何人都可以了解文件的路径吗? (我怀疑答案可能与 getting image height and width from zipped files 上的评论相同,但还有其他方法吗?)
更新
感谢 Baba,以下内容确实有效:
$fp = fopen('zip://C:\Users\<user>\<website>\test.zip#foo.jpg', "r");
(n.b. 只有当我给出 zip 文件的完整路径时我才能让它工作,而不是像通过表单上传文件时那样的 tmp 文件)。但是,尝试获取 mime 类型:echo mime_content_type($fp);
产生错误:
Warning: mime_content_type() [function.mime-content-type]: stream does not support seeking in C:\Users\<user>\<website>\includes\validation.php on line 70
无论文件类型如何,都会发生这种情况(即 http://php.net/manual/en/ziparchive.getstream.php 的唯一评论中所述的问题似乎对我没有影响)。
顺便说一句,这也是我尝试不同方法时遇到的相同错误:$fp = $zip->getStream('foo.jpg');
我知道在 SO 上还有其他几个“不支持流”的问题,但我无法弄清楚它们与我的问题有什么关系,我希望既然有人特别建议了这种方法,那么有人可能会有很好的建议回答...
(附注:我没有使用 finfo_*
函数,因为我的主机当前拒绝安装 PHP v5.3)。
最佳答案
一个。你可以从尝试开始
mime_content_type('zip:///path/to/file.zip#'. $chapterZip->getNameIndex ( $i ));
B.我现在唯一能想到的是 mime_content_type
的替代品,它可能不是最好的方法,但我相信它会浮出水面,直到我找到更好的解决方案
$chapterZip = new ZipArchive ();
if ($chapterZip->open ( "stockoverflow.zip" )) {
for($i = 0; $i < $chapterZip->numFiles; $i ++) {
var_dump ( mime_content_type_replacement ( $chapterZip->getNameIndex ( $i ) ) );
}
}
使用 文件扩展名
和 finfo_open ( FILEINFO_MIME )
的替换函数
function mime_content_type_replacement($filename) {
$mime_types = array (
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet'
);
$ext = pathinfo ( $filename, PATHINFO_EXTENSION );
if (array_key_exists ( $ext, $mime_types )) {
return $mime_types [$ext];
} elseif (function_exists ( 'finfo_open' )) {
$finfo = finfo_open ( FILEINFO_MIME );
$mimetype = finfo_file ( $finfo, $filename );
finfo_close ( $finfo );
return $mimetype;
} else {
return 'application/octet-stream';
}
}
更多的mime类型
PHP / Mime Types - List of mime types publically available?
编辑 1
刚刚测试了以下内容并且它有效
$fp = fopen('zip://C:\stockoverflow.zip#1.MOV.xml',"r");
编辑2
一个。 mime_content_type($fp)
将不起作用,因为 mime_content_type
仅接受字符串参数,请参阅 http://php.net/manual/en/function.mime-content-type.php
B.我不确定为什么你仍然坚持使用 mime_content_type
因为它也被折旧了
This function has been deprecated as the PECL extension Fileinfo provides the same functionality (and more) in a much cleaner way.
C.直接处理 $fileatt['tmp_name']
并不理想。它是一个临时文件,不能被操纵。如果你要处理该文件,你需要将它复制到服务器您的 PHP 将拥有访问它的完全权限
D. ZipArchive::getStream
仅适用于 zip
文件的本地副本,不适用于临时上传文件
关于php - 获取 mime 类型的压缩文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10363199/