我问过这个问题,发现PHP配置限制了我的上传到2MB,所以我将它固定为3MB。不过,我现在还有另外一个问题:我也在检查图片尺寸,如果图片超过3MB,则抛出以下错误。那么我怎样才能停止错误,并通过自己而不是PHP配置来检查大小?警告:getimagesize():文件名不能为空php
警告:和getimagesize():文件名不能为空
下面是代码:
$size = $_FILES['image']['size'];
list($width, $height) = getimagesize($_FILES['image']['tmp_name']);
$minh = 400;
$minw = 600;
$one_MB = 1024*1024; //1MB
if($size > ($one_MB*3))// this is not checking the size at all, the last echo is what I get if I try to upload over 3mb.
{
echo"File exceeds 3MB";
}
elseif ($width < $minw) {
echo "Image width shouldn't be less than 600px";
}
elseif ($height < $minh){
echo "Image height shouldn't be less than 400px";
}
else{
if (move_uploaded_file($_FILES['image']['tmp_name'], $new_location)){
//do something
}
else{
echo "Image is too big, try uploading below 3MB";
}
}
我已经尝试过与1.5MB相同的图像,它的工作原理和当我尝试3.5 MB时失败,所以它失败的原因是因为图像大小。我不明白的是为什么第一个'if'语句没有检查文件大小并让执行继续。我会尝试你的建议。 – user3006683
您应该在调用get_image_size()之前检查文件大小**。在执行任何错误处理之前,您的脚本失败。 – MrTweek