图像大小验证无效

时间:2014-12-10 06:15:43

标签: php

您好,我的表单有验证检查,检查现有图像,计数和

在IF条件下

尺寸,但在此处图像尺寸验证无效。

在我的代码中,我已经定义用户最多只能上传500 KB六张图片,但我在这里

无法上传六张图片,而这些图片总共只有340 KB。

以下是代码:

        $maxsize = '500000'; //Bytes    
        $errors = array();

    if (empty($_FILES['Upload_Property_Images']['name'][0])) 

    {
    $errors[] = 'You have not selected any image for uplaod field'; 
    }
    elseif ($_FILES['Upload_Property_Images']['size'] > $maxsize )

    {
    $errors[] = 'All six images must be less than 500 KB size'; 
    }

   else 
    {

    if (count($_FILES['Upload_Property_Images']['name']) !== 6)
        $errors[] = "-Your Only Allowed Six Images";


    $whitelist = array("jpg","png","JPG","PNG");

    foreach($_FILES['Upload_Property_Images']['name'] as $file) 
    {
        if ($file) 
        { 

            $temp = explode('.', $file);

            if (!in_array(end($temp), $whitelist )) 

            {
                $errors[] = "-All images must be JPG or PNG<br>";
                break;

            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

更新了代码段。

    $maxsize = '500000'; //Bytes    
    $errors = array();

if (empty($_FILES['Upload_Property_Images']['name'][0])) 

{
$errors[] = 'You have not selected any image for uplaod field'; 
}
elseif (array_sum($_FILES['Upload_Property_Images']['size']) > $maxsize )

{
$errors[] = 'All six images must be less than 500 KB size'; 
}

else 
{

if (count($_FILES['Upload_Property_Images']['name']) !== 6)
    $errors[] = "-Your Only Allowed Six Images";


$whitelist = array("jpg","png","JPG","PNG");

foreach($_FILES['Upload_Property_Images']['name'] as $file) 
{
    if ($file) 
    { 

        $temp = explode('.', $file);

        if (!in_array(end($temp), $whitelist )) 

        {
            $errors[] = "-All images must be JPG or PNG<br>";
            break;

        }
    }
}
}