gpt4 book ai didi

php - 如何在PHP中检查上传的文件类型

转载 作者:IT王子 更新时间:2023-10-29 01:06:37 27 4
gpt4 key购买 nike

我使用这段代码来检查图像的类型,

$f_type=$_FILES['fupload']['type'];

if ($f_type== "image/gif" OR $f_type== "image/png" OR $f_type== "image/jpeg" OR $f_type== "image/JPEG" OR $f_type== "image/PNG" OR $f_type== "image/GIF")
{
$error=False;
}
else
{
$error=True;
}

但一些用户提示他们在上传任何类型的图像时遇到错误,而另一些用户则没有遇到任何错误!

我想知道这是否能解决问题:

if (mime_content_type($_FILES['fupload']['type']) == "image/gif"){...

有什么意见吗?

最佳答案

永远不要使用 $_FILES..['type']。其中包含的信息根本没有经过验证,它是一个用户定义的值。自己测试类型。对于图片,exif_imagetype通常是一个不错的选择:

$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($_FILES['fupload']['tmp_name']);
$error = !in_array($detectedType, $allowedTypes);

或者,finfo functions很棒,如果你的服务器支持的话。

关于php - 如何在PHP中检查上传的文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6755192/

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