在PHP中上传多个文件时无法检查文件大小

时间:2017-05-14 13:44:48

标签: php file-upload

我试图检查多个文件上传的文件大小限制。每次收到抱歉,您的文件上传量太大,最大文件大小为10 MB。 无法弄清楚错误。

没有提供文件限制,文件上传工作正常。我还在PHP.ini文件中进行了必要的更改,如POST_MAX_SIZE和MAX_FILE_SIZE,但响应仍然相同。

foreach($_FILES['upload_slider']['tmp_name'] as $key => $tmp_name){
                #file upload size - file size 10MB
                $fileSize = $_FILES['upload_slider']['size'][$key];
                if ($fileSize <= 10485760) #10 MB 
                {

                    $_SESSION['upload_warning'] = "Sorry, your file is too large upload, maximum file size is 10 MB.";
                    header("location:../slider.php");
                    die;            
                }


            else{
                $file_name = strtolower($_FILES['upload_slider']['name'][$key]);
                $file_tmp = $_FILES['upload_slider']['tmp_name'][$key];
                $ext = pathinfo($file_name, PATHINFO_EXTENSION);
                $new_filename = rand().".".$ext;
                if(in_array($ext,$extension))
                {
                    move_uploaded_file($file_tmp= $_FILES['upload_slider']['tmp_name'][$key],"$album_dir$new_filename");
                    # insert record in database

                    $values = [
                    'slider_id' =>$slider_id,
                    'image_name' => $new_filename,
                    //'album_name' => $album_title,
                    'image_path' => $db_album_dir.$new_filename,
                    'uploaded_date' => date("Y/m/d h:i:s ")
                    ];
                    $res = $abc->insert($tablename,$values);                
                    header("location:../slider.php");
                }
                else
                {
                    $_SESSION['upload_warning'] = "Please upload appropriate file type extension i.e. <b> jpg, jpeg, png , gif. </b> ";
                    header("location:../slider.php");
                    die;
                }
            }

        }//EOF FROEACH

1 个答案:

答案 0 :(得分:1)

您是否尝试将每个文件限制为10Mb?因为现在你的if语句恰恰相反:

if ($fileSize <= 10485760){ //if the file is less or equal to 10mb
//return error
}
else{ //else if the file is larger than 10mb
//proceed with uploading
}

所以你应该这样试试:

if($fileSize >= 10485760){ //return error
}
else { //upload file
}