Can I trust the file type from $_FILES?

自作多情 提交于 2019-12-04 03:47:34

From the documentation:

The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.

No you cannot trust the $_FILES['userfile']['type'] variable. The value present in this variable could be forged. You can use finfo_file to detect file type more reliably:

$finfo = finfo_open(FILEINFO_MIME_TYPE); // we need mime type
echo finfo_file($finfo, "/path/to/uploaded/file"); // displays something like image/gif
finfo_close($finfo);

These functions require PHP >= 5.3.0.

Never trust anything that comes from the outside, especially file uploads!

Check the size, location, mime/type, extenstion and anything else you can check!

I always use the next function to check on valid images :

function Check_Image($Filename) {
    if ($Check_Image = @getimagesize($Filename)) {
        return TRUE;
    }
    return FALSE;
}

No, you cannot trust it because this information is provided by the client browser.

$_FILES['userfile']['type'] The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!