简体   繁体   English

getimagesize()vs finfo_file()是否与检测mime类型完全相同?

[英]getimagesize() vs finfo_file() the exact same for detecting mime type?

Are these functions doing the same checks to detect the mime type of a file? 这些功能是否进行相同的检查以检测文件的mime类型? Are there any benefits in choosing one over the other (ie terms of reliability/security)? 选择其中一个是否有任何好处(即可靠性/安全性)?

They are for different purpose, so you may use in they specific scope. 它们具有不同的用途,因此您可以在它们的特定范围内使用。 I'm prefer use finfo for the first check of the reliability of file if I don't know its origin 如果我不知道文件的finfo ,我更喜欢使用finfo来检查文件的可靠性

getimagesize — Get the size of an image getimagesize —获取图像的大小

Which also can gather the meta segment for JPG APP and presume to detect the image mime 可以收集JPG APP的元段并假定检测图像哑剧

And

finfo_file — Return information about a file finfo_file —返回有关文件的信息

Will get you the meta info from the file 将从文件中获取元信息

A s you marked GD, you may be interacting with images only(?). 如果您将其标记为GD,则可能仅与图像进行交互(?)。 So you may use getimagesize() , which seem to return the mime type of file independent of its extension, being a +1 on security, but probably limited by PHP supported images types. 因此,您可以使用getimagesize() ,它似乎返回的文件的mime类型与其扩展名无关,安全性为+1,但可能受PHP支持的图像类型的限制。

My PHP 5.6.4 has: 我的PHP 5.6.4具有:

    [IMAGETYPE_GIF] => 1
    [IMAGETYPE_JPEG] => 2
    [IMAGETYPE_PNG] => 3
    [IMAGETYPE_SWF] => 4
    [IMAGETYPE_PSD] => 5
    [IMAGETYPE_BMP] => 6
    [IMAGETYPE_TIFF_II] => 7
    [IMAGETYPE_TIFF_MM] => 8
    [IMAGETYPE_JPC] => 9
    [IMAGETYPE_JP2] => 10
    [IMAGETYPE_JPX] => 11
    [IMAGETYPE_JB2] => 12
    [IMAGETYPE_SWC] => 13
    [IMAGETYPE_IFF] => 14
    [IMAGETYPE_WBMP] => 15
    [IMAGETYPE_JPEG2000] => 9
    [IMAGETYPE_XBM] => 16
    [IMAGETYPE_ICO] => 17
    [IMAGETYPE_UNKNOWN] => 0
    [IMAGETYPE_COUNT] => 18

O n the other hand, I suggest you use finfo , to gatter real meta info of your file. Øn个另一方面,我建议你使用finfo ,到文件的gatter真正的元信息。

finfo do not read other thing than the headers, getimagesize can also return some markers and also extra info (extra computation), image specific info. finfo除了读取头文件外不读取其他内容, getimagesize还可以返回一些标记以及额外的信息(额外的计算), 图像特定的信息。

L ets check: 大号 ETS检查:

Having this simple non standar function: 具有以下简单的非标准功能:

function displayInfo($path, $info = FILEINFO_MIME_TYPE ) {

        $imageinfo = array();
        $getimagesize = array();

        $finfo = new finfo($info);
        $finfo_mt = $finfo->file($path);

        print_r(getimagesize($path, $imageinfo));
        print_r($imageinfo);
        print_r($finfo_mt);
    }

You may use $info=FILEINFO_MIME to return Type and Encoding for safe reading a file. 您可以使用$ info = FILEINFO_MIME返回Type和Encoding以安全地读取文件。 So, 所以,

display('file.php'); will output 将输出

Array
(
)
text/x-php

display('image.png.hide'); and display('image.png'); display('image.png'); will output 将输出

Array
(
    [0] => 31
    [1] => 31
    [2] => 3
    [3] => width="31" height="31"
    [bits] => 8
    [mime] => image/png
)
Array
(
)
image/png

and, display('image.jpg'); 并且display('image.jpg'); will output 将输出

Array
(
    [0] => 206
    [1] => 206
    [2] => 2
    [3] => width="206" height="206"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)
Array
(
    [APP0] => JFIF
    [APP13] => Photoshop 3.08BIMgldeLs_Kr6L1Vvu73FGOs(bFBMD01000ac1030000b1060000ad0b00006f0c0000800d0000fe11000011180000bf180000c8190000ef1a0000d2240000
    [APP2] => [REMOVED UTF CHARS FOR stackoverflow]
)
image/jpeg

Hope to be pointed on your question. 希望指出您的问题。

well there are basically two ways of finding the mime types of the files... one is mime_content_type and the other is file info... you can refer the urls bellow : 好吧,基本上有两种找到文件的mime类型的方法...一种是mime_content_type,另一种是文件信息...您可以引用以下网址:

http://php.net/manual/en/function.finfo-file.php http://php.net/manual/zh/function.finfo-file.php

http://php.net/manual/en/function.mime-content-type.php http://php.net/manual/zh/function.mime-content-type.php

mime_content_type is now deprecated .. file info method is for latest php versions... file info requires fileinfo extension to be enabled on the server... mime_content_type现在已弃用..文件信息方法适用于最新的php版本...文件信息需要在服务器上启用fileinfo扩展名...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM