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

报告相同问题?

悬赏问题

  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因
  • ¥20 python作业求过程
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式
  • ¥50 如何用python使用opencv里的cv::cudacodec::VideoWriter函数对视频进行GPU硬编码
  • ¥100 c#solidworks 二次开发 工程图自动标边线法兰 等折弯尺寸怎么标
  • ¥15 halcon DrawRegion 提示错误
  • ¥15 FastAPI Uvicorn启动显示404
  • ¥15 centos7.9脚本,怎么排除特定的访问记录
  • ¥15 关于#Django#的问题:我的静态文件呢?