[英]Getting mime type of file
I'm trying to get mime type of the profile picture they're in format user_id.jpg or gif or png, I experimented with this code but it's not working. 我试图获取配置文件图片的MIME类型,它们的格式为user_id.jpg或gif或png,我尝试了此代码,但无法正常工作。
function detectFileMimeType($filename='')
{
$filename = escapeshellcmd($filename);
$command = "file -b --mime-type -m /usr/share/misc/magic {$filename}";
$mimeType = shell_exec($command);
return trim($mimeType);
}
function get_avatar($image, $user_id, $account)
{
$imgurl ="http://mypage/files/pictures/picture-" . ($user_id, $mimeType) . ".jpg";
if (!is_imgurl_good($imgurl)) {
$imgurl = "http://mypage/sites/all/themes/simple_custom/user.png";
}
return $imgurl;
}
You should be able to get the MIME type of a file fairly easily with built in functions provided by PHP - 使用PHP提供的内置函数,您应该能够相当容易地获得文件的MIME类型-
function detectFileMimeType($filename='')
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type
$fileglob = glob($filename);
echo finfo_file($finfo, $fileglob);
finfo_close($finfo);
}
http://php.net/manual/en/function.finfo-file.php http://php.net/manual/zh/function.finfo-file.php
http://php.net/manual/en/function.glob.php http://php.net/manual/zh/function.glob.php
As well as finfo
which works fine, a slightly easier method is to get PHP getimagesize
function return, as this will return the MIME type as extracted from the file: 除了finfo
可以正常工作外,一种稍微简单的方法是使PHP getimagesize
函数返回,因为这将返回从文件中提取的MIME类型:
$file = "blah/blahblah/bubbles.jpg";
$fileTypeData = getimagesize($file);
$fileTypeData['mime'] = The Mime type of the image file.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.