dran0703 2014-12-10 06:15
浏览 12
已采纳

图像大小验证无效

Hello I have a validation check for my form which is checking the images existing, count and

size in a IF condition but in here the image size validation is not working.

In my code I have defined that users can only upload up to 500 KB six images size but here I am

unable to upload six images which total all of them are only 340 KB.

Here is the Code:

        $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条回答 默认 最新

  • doulongdan2264 2014-12-10 06:35
    关注

    Updated Code Snippet.

        $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;
    
            }
        }
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据