gpt4 book ai didi

php - $_FILES ['type' ] 是由客户端还是服务器设置的?

转载 作者:行者123 更新时间:2023-12-04 04:54:27 25 4
gpt4 key购买 nike

问题:
$_FILES[...]['type'] 是直接来自客户端还是 PHP/Apache 设置了这个值?
为什么我问这个:
这个是在编写应该检查上传文件类型的上传脚本时弹出的,我已经有了检查上传文件 mime 类型的代码,但我需要那个代码吗?
一些与问题相关的代码:
这是我目前在上传文件验证方法中得到(简化)的内容,这应该检查上传文件的类型是否真的是客户声称的类型:

// Get temporary file and mime type
$src = $file["tmp_name"];
$mime = $file['type'];

// Get real file mime type, this does not guarantee that file is valid
$finfo = new finfo();
$file_mime = $finfo->file( $src, FILEINFO_MIME_TYPE );
unset($finfo);

// Test mime types against each other
if ($file_mime === $http_mime)
return true;

最佳答案

PHP documentatio n,这个信息来自浏览器:

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.



但是,正如文档和关于您的问题的其他评论所述,您不会对这些信息非常有信心:某些浏览器只是没有填写该字段,它可能被黑客入侵等...

处理 mime 类型的可靠方法是在实际文件上传到服务器后检查它们。

这些问题已经很好地讨论了 on Stack Overflow如果你想了解详情。

关于php - $_FILES ['type' ] 是由客户端还是服务器设置的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16959282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com