How to check uploaded file type in PHP(如何在PHP中检查上传的文件类型)
问题描述
我用这段代码来检查图像的类型,
I used this code to check for the type of images,
$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;
}
但有些用户抱怨他们在上传任何类型的图像时出错,而有些用户则没有出错!
but some users complain they get an error while uploading any type of images, while some others don't get any errors!
我想知道这是否能解决问题:
I was wondering if this fixes the problem:
if (mime_content_type($_FILES['fupload']['type']) == "image/gif"){...
有什么意见吗?
推荐答案
永远不要使用 $_FILES..['type']
.其中包含的信息根本没有经过验证,它是用户定义的值.自己测试类型.对于图片,exif_imagetype
通常是一个不错的选择选择:
Never use $_FILES..['type']
. The information contained in it is not verified at all, it's a user-defined value. Test the type yourself. For images, exif_imagetype
is usually a good choice:
$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($_FILES['fupload']['tmp_name']);
$error = !in_array($detectedType, $allowedTypes);
或者,finfo
函数 也很棒,如果您的服务器支持它们.
Alternatively, the finfo
functions are great, if your server supports them.
这篇关于如何在PHP中检查上传的文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在PHP中检查上传的文件类型


基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01