file-upload - PHP 文件上传大于 upload_max_filesize 和错误

标签 file-upload php

如果上传到我的 Web 服务器的文件大于 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.';
}

我的 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

最佳答案

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


资源:

关于file-upload - PHP 文件上传大于 upload_max_filesize 和错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3762467/

相关文章:

file-upload - Yii 表单生成器

c# - 文件上传,远程服务器返回错误 : (413) Request Entity Too Large

php - 使用 Try/Catch PHP 方法捕获 Stripe 错误

php - FLOW3 Action 参数和对象数组

php - 从 PHP 访问 Gmails 垃圾邮件文件夹

php - 如何使为调整图像大小而编写的代码适用于各种图像扩展并对其进行优化?

javascript - dropzonejs 文件类型

php 为什么我们通过电子邮件发送的 pdf 打不开?

php mysql,更新查询不起作用

javascript - 如何在上传之前获取文件名并将其传递到另一个文本输入字段而不使用 fakepath