PHP 如何检查文件是 mp3 还是图像文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2006632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP how can i check if a file is mp3 or image file?
提问by kmunky
how can i check if a file is an mp3 file or image file, other than check each possible extension?
除了检查每个可能的扩展名之外,我如何检查文件是 mp3 文件还是图像文件?
采纳答案by Pekka
You can identify image files using getimagesize.
您可以使用getimagesize.
To find out more about MP3 and other audio/video files, I have been recommended php-mp4infogetID3().
要了解有关 MP3 和其他音频/视频文件的更多信息,我被推荐使用php-mp4infogetID3()。
回答by Gordon
Native ways to get the mimetype:
获取 mimetype 的本地方法:
For PHP < 5.3 use mime_content_type()
For PHP >= 5.3 use finfo_fopen()
对于 PHP < 5.3 使用mime_content_type()
对于 PHP >= 5.3 使用finfo_fopen()
Alternatives to get the MimeType are exif_imagetypeand getimagesize, but these rely on having the appropriate libs installed. In addition, they will likely just return image mimetypes, instead of the whole list given in magic.mime.
获取 MimeType 的替代方法是exif_imagetype和getimagesize,但这些依赖于安装了适当的库。此外,它们可能只会返回图像 mimetypes,而不是在magic.mime中给出的整个列表。
If you don't want to bother about what is available on your system, just wrap all four functions into a proxy method that delegates the function call to whatever is available, e.g.
如果您不想打扰系统上可用的内容,只需将所有四个函数包装到一个代理方法中,该方法将函数调用委托给可用的任何内容,例如
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;
}
回答by Alix Axel
To find the mime type of a file I use the following wrapper function:
要查找文件的 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('~^(.+);.*$~', '', mime_content_type($path));
}
else if (function_exists('exif_imagetype') === true)
{
$result = image_type_to_mime_type(exif_imagetype($path));
}
}
return $result;
}
回答by Burntime
<?php
echo mime_content_type('php.gif') . "\n";
echo mime_content_type('test.php');
?>
Output:
输出:
image/gif
text/plain
图像/gif
文本/普通
Or better use finfo_file()the other way is deprecated.
或者更好地使用finfo_file()其他方式已弃用。
回答by Deepan Prabhu Babu
getimageinfo is best to find images . Check if return type is false .
getimageinfo 最好查找图像。检查返回类型是否为 false 。
回答by catsby
You can use FileInfo module which is built into PHP since 5.3. If you are using a PHP version less than PHP 5.3, you can install it as a PECL extension:
您可以使用自 5.3 起内置于 PHP 中的 FileInfo 模块。如果您使用的 PHP 版本低于 PHP 5.3,您可以将其安装为 PECL 扩展:
After installation the finfo_filefunction will return file information.
安装后该finfo_file函数将返回文件信息。
PECL extension: http://pecl.php.net/package/fileinfo
PECL 扩展名:http: //pecl.php.net/package/fileinfo
PHP Documentation: http://www.php.net/manual/en/book.fileinfo.php
回答by Richy B.
You could use finfolike this:
你可以像这样使用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);
or if you have problems with finfo not being installed, or the mime magic file just not working (it works correctly on 3 out of our 4 servers - all identical OS and PHP installs) - then try using Linux's native file (don't forget to sanitise the filename though: in this example, I know the filename can be trusted as it's a PHP temporary filename in my test code):
或者,如果您在未安装 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]);
}
Then just pass the mime file type to a switch statement like:
然后只需将 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;
回答by JaseC
This function checks if the file is an image based on extension and mime and returns true if it's a browser compatible image...
此函数检查文件是否是基于扩展名和 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;
}
回答by Hoàng V? Tgtt
For Images, I use:
对于图像,我使用:
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;
}
回答by Alex Langer
The best way is to use finfo_file function. Example:
最好的方法是使用 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);
}
?>

