PHP文件上传大于upload_max_filesize并出现错误

3
如何在上传到我的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

你的Web服务器是否记录任何错误?你是否超过了“max_input_time”? - Wrikken
我无法理解为什么会超过最大输入时间,因为我连接的是千兆局域网到服务器,而且一个大于6MB的文件几乎瞬间就传输完成了。不过我会进一步调查的。 - Chris
1个回答

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

资源:


它总是'userfile'吗?还是它会是输入字段的名称? - Chris
它基于字段名称。对于您来说,它将是 $_FILES['file']['error'] - Colin Hebert
在检查提交按钮是否被按下后,我有以下代码:if($_FILES['file']['error'] === UPLOAD_ERR_OK){echo "error";}else{echo "no error";} 但是我的脚本没有输出任何值。 - Chris
3
请检查 post_max_size 是否大于 upload_max_filesize。如果这两个值相同且文件超过限制,则会导致 $_FILES 数组(和 $_POST)为空。详见 http://www.php.net/manual/zh/ini.core.php#ini.post-max-size。 - Chuck Burgess

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接