php中怎样在文件中选择图片大小,在php中上传文件之前检查图片文件的类型和大小...

请注意,您可能不希望依赖文件扩展名来确定文件类型。例如,某人上传具有.png扩展名的可执行文件相当容易。恶意客户端也可以轻易伪造mime类型作为图像传递。依赖该信息是一种安全风险。

PHP 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.

尝试使用gd(getimagesize())加载图像以确保它们实际上是有效图像(而不仅仅是假装有图像文件标题的随机文件...... finfo_file依赖于这些标题)。

if($_FILES["imagefile"]["size"] >= 2120000) {

echo "F2";

die();

} else {

$imageData = @getimagesize($_FILES["imagefile"]["tmp_name"]);

if($imageData === FALSE || !($imageData[2] == IMAGETYPE_GIF || $imageData[2] == IMAGETYPE_JPEG || $imageData[2] == IMAGETYPE_PNG)) {

echo "F2";

die();

}

}

如果您确实必须使用扩展来验证文件是否为图像,请使用strtolower()将扩展名设置为小写。

$filecheck = basename($_FILES['imagefile']['name']);

$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));

if (!(($ext == "jpg" || $ext == "gif" || $ext == "png") && ($_FILES["imagefile"]["type"] == "image/jpeg" || $_FILES["imagefile"]["type"] == "image/gif" || $_FILES["imagefile"]["type"] == "image/png") &&

($_FILES["imagefile"]["size"] < 2120000))){

echo "F2";

die();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值