有没有一种快速而简单的方法,在PHP中将MIME类型映射到文件扩展名?
有没有一种快速而简单的方法,在PHP中将MIME类型映射到文件扩展名?
虽然没有内置功能,但自己编写并不是特别难:
function system_extension_mime_types() {
# Returns the system MIME type mapping of extensions to MIME types, as defined in /etc/mime.types.
$out = array();
$file = fopen('/etc/mime.types', 'r');
while(($line = fgets($file)) !== false) {
$line = trim(preg_replace('/#.*/', '', $line));
if(!$line)
continue;
$parts = preg_split('/\s+/', $line);
if(count($parts) == 1)
continue;
$type = array_shift($parts);
foreach($parts as $part)
$out[$part] = $type;
}
fclose($file);
return $out;
}
function system_extension_mime_type($file) {
# Returns the system MIME type (as defined in /etc/mime.types) for the filename specified.
#
# $file - the filename to examine
static $types;
if(!isset($types))
$types = system_extension_mime_types();
$ext = pathinfo($file, PATHINFO_EXTENSION);
if(!$ext)
$ext = $file;
$ext = strtolower($ext);
return isset($types[$ext]) ? $types[$ext] : null;
}
function system_mime_type_extensions() {
# Returns the system MIME type mapping of MIME types to extensions, as defined in /etc/mime.types (considering the first
# extension listed to be canonical).
$out = array();
$file = fopen('/etc/mime.types', 'r');
while(($line = fgets($file)) !== false) {
$line = trim(preg_replace('/#.*/', '', $line));
if(!$line)
continue;
$parts = preg_split('/\s+/', $line);
if(count($parts) == 1)
continue;
$type = array_shift($parts);
if(!isset($out[$type]))
$out[$type] = array_shift($parts);
}
fclose($file);
return $out;
}
function system_mime_type_extension($type) {
# Returns the canonical file extension for the MIME type specified, as defined in /etc/mime.types (considering the first
# extension listed to be canonical).
#
# $type - the MIME type
static $exts;
if(!isset($exts))
$exts = system_mime_type_extensions();
return isset($exts[$type]) ? $exts[$type] : null;
}
fileinfo
,网址是http://www.php.net/manual/en/ref.fileinfo.php。 - Jorge Barata您可以使用mime_content_type
,但它已经被弃用了。现在请使用fileinfo
代替它:
function getMimeType($filename) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mime;
}
finfo_file()
和mime_content_type()
需要文件存在。 - Pangtext/plain
。具有讽刺意味的是,即使是PHP内置服务器也避免使用这些函数,并拥有自己的映射表:https://github.com/php/php-src/blob/d3c86527a5a64ea8fe8279e5099960ede7e3b731/sapi/cli/mime_type_map.h - Christian$mimes = new \Mimey\MimeTypes;
// Convert extension to MIME type:
$mimes->getMimeType('json'); // application/json
// Convert MIME type to extension:
$mimes->getExtension('application/json'); // json
如果您只使用非常简单的图像扩展,并且需要一些非常简单的东西,那么我认为这已经足够了。
switch($info['mime'])
{
case 'image/gif' : $extension = 'gif'; break;
case 'image/png' : $extension = 'png'; break;
case 'image/jpeg' : $extension = 'jpg'; break;
default :
throw new ApplicationException('The file uploaded was not a valid image file.');
break;
}
使用此文件: https://github.com/ralouphie/mimey/blob/develop/mime.types.php
像这样:
$mimes=include('mime.types.php');
或者复制内容:
$mime= array (
'mimes' =>
array (
'ez' =>
array (
0 => 'application/andrew-inset',
),
'aw' =>
array (
0 => 'application/applixware',
),
'atom' =>
array (
0 => 'application/atom+xml',
),
'atomcat' =>
array (
0 => 'application/atomcat+xml',
)
...
以及从流中获取的示例:
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mime=$finfo->buffer($data);
$mimes=include(__DIR__."/mime.types.php");
echo ($mime); //mime
echo ($mimes['extensions'][$mime]); // file extension