PHP의 MIME 유형과 관련된 확장자를 어떻게 결정합니까?
-
18-09-2019 - |
문제
MIME 유형의 빠르고 더러운 매핑을 사용할 수있는 PHP의 확장에 대한 매핑이 있습니까?
해결책
내장되지는 않았지만 자신의 것을 굴리는 것은 매우 어렵지 않습니다.
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;
}
다른 팁
당신은 사용할 수 있습니다 mime_content_type
그러나 더 이상 사용되지 않습니다. 사용 fileinfo
대신에:
function getMimeType($filename) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mime;
}
이 라이브러리를 사용하고 싶을 수도 있습니다. https://github.com/ralouphie/mimeyy
예제 사용 :
$mimes = new \Mimey\MimeTypes;
// Convert extension to MIME type:
$mimes->getMimeType('json'); // application/json
// Convert MIME type to extension:
$mimes->getExtension('application/json'); // json
이것은 제공된 PHP 기능의 품질이 모호하기 때문입니다.
웹 서비스를 사용하려면 Mimetype <-> Icon Service의 일부로 이것을 만들었습니다.
예를 들어 :
Appengine에서 실행되므로 고 가용성이 있어야합니다.
제한된 제한된 이미지 확장 기능을 사용하고 매우 간단한 것이 필요하다면 이것이 충분하다고 생각합니다.
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
제휴하지 않습니다 StackOverflow