简体   繁体   English

获取在PHP和WordPress中没有扩展名的图像的文件类型

[英]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.

相关问题 获取 PHP 文件/图像扩展名 - Get Php file/images extension 如何使用wordpress在没有.php文件扩展名的情况下获取get_page_template_slug()? - How do I get get_page_template_slug() without the .php file extension with wordpress? 如何在Wordpress中设置一些页面以具有.php扩展名? - How Do I Set Some Pages In Wordpress To Have The .php Extension? 如果我只有文件名,如何在PHP中获得文件扩展名? - How do I get a File Extension in PHP if I only have the File Name? PHP file_put_contents获取文件扩展名/类型 - PHP file_put_contents get file extension/type WordPress提要中的内容包含垃圾社交图片-如何删除? PHP字符串或Wordpress - Wordpress feeds have rubbish social images in content - how do I remove? Either PHP string or Wordpress 删除php文件扩展名以及目录格式的_GET参数 - Remove php file extension as well as have _GET param in a directory format 获取没有PECL扩展名的PHP5中本地文件的MIME类型? - Get MIME type of a local file in PHP5 without a PECL extension? 在PHP中获取照片文件扩展名类型的最佳方法 - Best way to get a photo's file extension type in PHP 让Apache / PHP根据文件扩展名设置MIME类型,以将文件作为PHP执行 - Have Apache/PHP set MIME type based upon file extension for files being executed as PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM