PHP 文件上传大于 upload_max_filesize 并出错

Chr*_*ris 3 php file-upload

如何捕获上传到我的网络服务器的大于 php upload_max_filesize 的文件的错误?

我的问题与so/large-file-upload-errors-with-php类似,尽管我的内存限制设置为 512M,所以该问题使用的分辨率对我没有帮助。

例如,我尝试上传 6.9MB 的文件,我的 upload_max_filesize = 6M。我的脚本基本上停止执行,我不知道在哪里或为什么。另外,我打开了错误报告。

另外我应该注意,我可以使用以下代码正确上传和处理 <6MB 的文件:

if(isset($_FILES['file']['name']) and !empty($_FILES['file']['name'])){

    $info = pathinfo($_FILES['file']['name']); 
    $ext = $info['extension'];
    //verify file is of allowed types 
    if(Mimetypes::isAllowed($ext)){
        if(filesize($_FILES['file']['tmp_name']) <= AttachmentUploader::$maxFilesize){              
            $a = new AttachmentUploader();      //for file uploading 
            if($a->uploadFile($_FILES['file'], 'incident', $_POST['sys_id'])){
                header("location: ".$links['status']."?item=incident&action=update&status=1&place=".urlencode($links['record']."id=".$_POST['sys_id']));            
            }else{
                header("location: ".$links['status']."?item=incident&action=update&status=-1&place=".urlencode($links['record']."id=".$_POST['sys_id']));           
            }

        }else{      
            $errors[] = 'The file you attempted to upload is too large.  0.5MB is the maximum allowed size for a file.  If you are trying to upload an image, it may need to be scaled down.';
        }
    }else{
        $errors[] = 'The file you attempted to upload is not allowed.  Acceptable extensions: jpg, gif, bmp, png, xls, doc, docx, txt, pdf';
    }
}else{
    $errors[] = 'Please attach a file.';
}
Run Code Online (Sandbox Code Playgroud)

我的 php.ini 设置:

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 7200     ; Maximum execution time of each script, in seconds
memory_limit = 512M  ; Maximum amount of memory a script may consume (8MB)


;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
upload_tmp_dir = /tmp

; Maximum allowed size for uploaded files.
upload_max_filesize = 6M
Run Code Online (Sandbox Code Playgroud)

Col*_*ert 5

错误位于$_FILES['userfile']['error']. 您只需检查该值UPLOAD_ERR_INI_SIZE来检测文件是否大于 php.ini 中定义的最大大小。


资源 :

  • 检查 post_max_size 是否大于 upload_max_filesize。如果这些值相同并且文件超出限制,则会导致 $_FILES 数组(和 $_POST)为空。http://www.php.net/manual/en/ini.core.php#ini.post-max-size (3认同)