[英]Get file type for images that do not have an extension in PHP and WordPress
I am creating a website/theme using WordPress. 我正在使用WordPress创建网站/主题。 I am retrieving a number of images from various websites. 我正在从各个网站检索许多图像。 Some images do not come with extensions, but they are rendered correctly in the browser. 有些图像没有扩展名,但是可以在浏览器中正确显示。
All of that is fine, but I also need to get the file type, and I am using this: 一切都很好,但是我还需要获取文件类型,并且我正在使用此文件:
$filetype = wp_check_filetype(basename($image_file_name), null );
Now, if the file name of the image retrieved does have an extension, say jpg, then the output of the above call is 现在,如果检索到的图像的文件名确实具有扩展名,例如jpg,则上述调用的输出为
Array
(
[ext] => jpg
[type] => image/jpeg
)
However if the extension is not part of the file name then the call above returns the following array 但是,如果扩展名不是文件名的一部分,则上面的调用将返回以下数组
Array
(
[ext] =>
[type] =>
)
Obviously something is not right, I may have to change how to check for the file type rather than rely on the extension, but I do not have an idea how to do this. 显然有些事情是不对的,我可能不得不更改如何检查文件类型而不是依赖扩展名,但是我不知道如何执行此操作。 How do I check that? 我该如何检查? How do I retrieve the file type from an image that does not have an extension? 如何从没有扩展名的图像中检索文件类型?
Thanks. 谢谢。
Try to use finfo_file . 尝试使用finfo_file 。 I've used it to check the type of a file uploaded through a form. 我用它来检查通过表单上传的文件的类型。 Here's the code that I've used in my application 这是我在应用程序中使用的代码
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, 'name_of_your_file_with_or_without_extension');
Hope this will help :). 希望这会有所帮助:)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.