PHP- 通过 URL 获取文件类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25918761/
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- Get file type by URL
提问by Ashish Srivastava
I want to get the file type (eg. image/gif) by URL using PHP.I had tried
我想使用 PHP.I 尝试通过 URL 获取文件类型(例如图像/gif)
<?php
$image_path="http://fc04.deviantart.net/fs71/f/2010/227/4/6/PNG_Test_by_Destron23.png";
exif_imagetype($image_path);
?>
The above code gave me a blank page and the following code returned "3":
上面的代码给了我一个空白页,下面的代码返回“3”:
<?php
$image_path="http://fc04.deviantart.net/fs71/f/2010/227/4/6/PNG_Test_by_Destron23.png";
echo exif_imagetype($image_path);
?>
Where am I going wrong? Solved: using Fileinfo to fetch content type
我哪里错了?已解决:使用 Fileinfo 获取内容类型
采纳答案by Avis
<?php
$image_path="http://fc04.deviantart.net/fs71/f/2010/227/4/6/PNG_Test_by_Destron23.png";
echo exif_imagetype($image_path);
?>
It returned 3
because png
response type as maciej said.
它返回3
是因为png
maciej 所说的响应类型。
Try this to get like this image/png
:
试试这个得到这样的image/png
:
echo mime_content_type($image_path);
Try this:
尝试这个:
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
echo finfo_file($finfo, $image_path) . "\n";
finfo_close($finfo);
回答by pgee70
Here is a PHP function I came up with:
这是我想出的一个 PHP 函数:
/**
* @param $image_path
* @return bool|mixed
*/
function get_image_mime_type($image_path)
{
$mimes = array(
IMAGETYPE_GIF => "image/gif",
IMAGETYPE_JPEG => "image/jpg",
IMAGETYPE_PNG => "image/png",
IMAGETYPE_SWF => "image/swf",
IMAGETYPE_PSD => "image/psd",
IMAGETYPE_BMP => "image/bmp",
IMAGETYPE_TIFF_II => "image/tiff",
IMAGETYPE_TIFF_MM => "image/tiff",
IMAGETYPE_JPC => "image/jpc",
IMAGETYPE_JP2 => "image/jp2",
IMAGETYPE_JPX => "image/jpx",
IMAGETYPE_JB2 => "image/jb2",
IMAGETYPE_SWC => "image/swc",
IMAGETYPE_IFF => "image/iff",
IMAGETYPE_WBMP => "image/wbmp",
IMAGETYPE_XBM => "image/xbm",
IMAGETYPE_ICO => "image/ico");
if (($image_type = exif_imagetype($image_path))
&& (array_key_exists($image_type ,$mimes)))
{
return $mimes[$image_type];
}
else
{
return FALSE;
}
}
回答by Gentle153
You are not going wrong anywhere. exif_imagetype
returns the value of one of the image type constants: http://php.net/manual/en/image.constants.php
你在任何地方都不会出错。exif_imagetype
返回图像类型常量之一的值:http: //php.net/manual/en/image.constants.php
If you would like to convert this to an extension string, you could use a switch statement:
如果要将其转换为扩展字符串,可以使用 switch 语句:
$typeString = null;
$typeInt = exif_imagetype($image_path);
switch($typeInt) {
case IMG_GIF:
$typeString = 'image/gif';
break;
case IMG_JPG:
$typeString = 'image/jpg';
break;
case IMG_JPEG:
$typeString = 'image/jpeg';
break;
case IMG_PNG:
$typeString = 'image/png';
break;
case IMG_WBMP:
$typeString = 'image/wbmp';
break;
case IMG_XPM:
$typeString = 'image/xpm';
break;
default:
$typeString = 'unknown';
}
You may want to change the order to most to least frequently expected for better performance.
您可能希望将顺序更改为最频繁到最不常见,以获得更好的性能。
回答by Alex Howansky
In the first example, you're getting a blank page because you're not doing anything with the return value from the function call. In the second example, you're getting a valid response. See the manual page for exif_imagetype()for a list of what the values mean.
在第一个例子中,你得到一个空白页,因为你没有对函数调用的返回值做任何事情。在第二个示例中,您将获得有效响应。有关值的含义列表,请参阅exif_imagetype() 的手册页。
回答by Grice
exif_imagetypereturns the image type. The response, 3, indicates it is IMAGETYPE_PNG, the correct response.
exif_imagetype返回图像类型。响应 3 表示它是 IMAGETYPE_PNG,即正确的响应。
回答by Maciej Asembler
3 is image type response for PNG image. See: http://php.net/manual/en/function.exif-imagetype.php
3 是 PNG 图像的图像类型响应。请参阅:http: //php.net/manual/en/function.exif-imagetype.php