我试图获得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;
}
您应该能够通过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/en/function.glob.php 和finfo
一样,一个稍微简单的方法是获得PHP getimagesize
函数返回,因为这将返回从文件中提取的MIME类型:
$file = "blah/blahblah/bubbles.jpg";
$fileTypeData = getimagesize($file);
$fileTypeData['mime'] = The Mime type of the image file.