【发布时间】:2013-11-17 16:38:16
【问题描述】:
除了检查每个可能的扩展名之外,我如何检查文件是 mp3 文件还是图像文件?
【问题讨论】:
标签: php mime-types
除了检查每个可能的扩展名之外,我如何检查文件是 mp3 文件还是图像文件?
【问题讨论】:
标签: php mime-types
此函数获取文件路径,如果支持使用finfo_open 和mime_content_type,则返回image 或video 或audio 字符串。
/**
* get file type
* @return image, video, audio
*/
public static function getFileType($file)
{
if (function_exists('finfo_open')) {
if ($info = finfo_open(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME)) {
$mimeType = finfo_file($info, $file);
}
} elseif (function_exists('mime_content_type')) {
$mimeType = mime_content_type($file);
}
if (strstr($mimeType, 'image/')) {
return 'image';
} else if (strstr($mimeType, 'video/')) {
return 'video';
} else if (strstr($mimeType, 'audio/')) {
return 'audio';
} else {
return null;
}
}
【讨论】:
获取 mimetype 的原生方法:
对于 PHP mime_content_type()
对于 PHP >= 5.3 使用 finfo_fopen()
获取 MimeType 的替代方法是 exif_imagetype 和 getimagesize,但这些依赖于安装适当的库。此外,它们可能只返回图像 mimetypes,而不是 magic.mime 中给出的整个列表。
如果您不想担心系统上可用的内容,只需将所有四个函数包装到一个代理方法中,该方法将函数调用委托给可用的任何内容,例如
function getMimeType($filename)
{
$mimetype = false;
if(function_exists('finfo_fopen')) {
// open with FileInfo
} elseif(function_exists('getimagesize')) {
// open with GD
} elseif(function_exists('exif_imagetype')) {
// open with EXIF
} elseif(function_exists('mime_content_type')) {
$mimetype = mime_content_type($filename);
}
return $mimetype;
}
【讨论】:
最好的方法是使用 finfo_file 函数。 示例:
<?php
if (isset($_FILES['yourfilename']['tmp_name'])) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['yourfilename']['tmp_name']);
if ($mime == 'image/jpg') {
echo "It's an jpg image!";
}
finfo_close($finfo);
}
?>
【讨论】:
对于图片,我使用:
function is_image($path)
{
$a = getimagesize($path);
$image_type = $a[2];
if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP)))
{
return true;
}
return false;
}
【讨论】:
此函数根据扩展名和mime检查文件是否为图像,如果是浏览器兼容的图像则返回true...
function checkImage($image) {
//checks if the file is a browser compatible image
$mimes = array('image/gif','image/jpeg','image/pjpeg','image/png');
//get mime type
$mime = getimagesize($image);
$mime = $mime['mime'];
$extensions = array('jpg','png','gif','jpeg');
$extension = strtolower( pathinfo( $image, PATHINFO_EXTENSION ) );
if ( in_array( $extension , $extensions ) AND in_array( $mime, $mimes ) ) return TRUE;
else return FALSE;
}
【讨论】:
getimageinfo 最好是查找图像。 检查返回类型是否为 false 。
【讨论】:
要查找文件的 mime 类型,我使用以下包装函数:
function Mime($path)
{
$result = false;
if (is_file($path) === true)
{
if (function_exists('finfo_open') === true)
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if (is_resource($finfo) === true)
{
$result = finfo_file($finfo, $path);
}
finfo_close($finfo);
}
else if (function_exists('mime_content_type') === true)
{
$result = preg_replace('~^(.+);.*$~', '$1', mime_content_type($path));
}
else if (function_exists('exif_imagetype') === true)
{
$result = image_type_to_mime_type(exif_imagetype($path));
}
}
return $result;
}
【讨论】:
你可以像这样使用finfo:
$mime = finfo_open(FILEINFO_MIME, $path_to_mime_magic_file);
if ($mime ===FALSE) {
throw new Exception ('Finfo could not be run');
}
$filetype = finfo_file($mime, $filename);
finfo_close($mime);
或者,如果您遇到了未安装 finfo 的问题,或者 mime 魔术文件无法正常工作(它在我们的 4 台服务器中的 3 台上正常工作 - 所有操作系统和 PHP 安装相同) - 然后尝试使用 Linux 的本机文件(不要不过不要忘记清理文件名:在这个例子中,我知道文件名是可以信任的,因为它是我的测试代码中的 PHP 临时文件名):
ob_start();
system('file -i -b '.$filename);
$output = ob_get_clean();
$output = explode("; ", $output);
if (is_array($output)) {
$filetype = trim($output[0]);
}
然后只需将 mime 文件类型传递给 switch 语句,例如:
switch (strtolower($filetype)) {
case 'image/gif':
return '.gif';
break;
case 'image/png':
return '.png';
break;
case 'image/jpeg':
return '.jpg';
break;
case 'audio/mpeg':
return '.mp3';
break;
}
return null;
【讨论】:
<?php
echo mime_content_type('php.gif') . "\n";
echo mime_content_type('test.php');
?>
输出:
图片/gif
文本/纯文本
或者更好地使用finfo_file(),另一种方式已弃用。
【讨论】:
mime_content_type() 实际上从未被弃用。文档中只是a bug。
您可以使用 PHP 5.3 以来内置的 FileInfo 模块。如果您使用的是低于 PHP 5.3 的 PHP 版本,则可以将其安装为 PECL 扩展:
安装后finfo_file函数会返回文件信息。
【讨论】:
您可以使用getimagesize 识别图像文件。
要了解有关 MP3 和其他音频/视频文件的更多信息,我已被推荐 php-mp4info getID3()。
【讨论】: