PHP文件上传大于upload_max_filesize和错误 [英] PHP File Upload greater than upload_max_filesize and error

查看:122
本文介绍了PHP文件上传大于upload_max_filesize和错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的问题类似于所以/ large-file-upload-errors-with-php ,尽管我的内存限制设置为512M,所以使用的问题的解决方案不会帮助我。



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



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

<$ p ($ _ FILES ['file'] ['name'])和!empty($ _ FILES ['file'] ['name'])){

$ info = pathinfo($ _ FILES ['file'] ['name']);
$ ext = $ info ['extension'];
//验证文件是允许的类型
if(Mimetypes :: isAllowed($ ext)){
if(filesize($ _ FILES ['file'] ['tmp_name'])< ; = AttachmentUploader :: $ maxFilesize){
$ a = new AttachmentUploader(); //文件上传
if($ a-> uploadFile($ _ FILES ['file'],'incident',$ _POST ['sys_id'])){
header(location: 。$链接[ '状态'] 项=事件&放大器;行动=更新和放大器;状态= 1&安培;地点=。用urlencode(。$链接[ '记录'] ID = $ _ POST [ 'sys_id']) );
} else {
header(location:。$ links ['status']。item = incident& action = update& status = -1&place; =。urlencode($ links [ '记录'] ID = $ _ POST [ 'sys_id']))。。
}

}其他{
$ errors [] ='您尝试上传的文件太大。 0.5MB是文件允许的最大大小。如果您尝试上传图片,则可能需要缩小图片。
}
} else {
$ errors [] ='您试图上传的文件是不允许的。可接受的扩展名:jpg,gif,bmp,png,xls,doc,docx,txt,pdf';
}
} else {
$ errors [] ='请附上档案。

$ / code>

我的php.ini设置:

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
;资源限制;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

max_execution_time = 7200;每个脚本的最大执行时间,以秒为单位
memory_limit = 512M;脚本可能消耗的最大内存量(8MB)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;文件上传;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;是否允许HTTP文件上传
file_uploads =在

; HTTP上传文件的临时目录(如果不是
,则使用系统默认值)。
upload_tmp_dir = / tmp

;上传文件的最大允许大小。
upload_max_filesize = 6M


解决方案

$ _ FILES [ 'userfile的'] [ '错误'] 。你只需要检查这个值是否小于code> UPLOAD_ERR_INI_SIZE 来检测文件是否大于php.ini中定义的最大大小。



资源
$ b


How can I catch an error on a file uploaded to my web server that is greater than php upload_max_filesize?

My question is similar to so/large-file-upload-errors-with-php although my memory limit is set to 512M so the resolution that question used will not help me.

I am trying to upload a file 6.9MB for example and my upload_max_filesize = 6M. My script basically just stops executing and I cannot tell where or why. Also, I have error reporting turned on.

Also I should note that files <6MB I can upload and process correctly with the following code:

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.';
}

My php.ini settings:

;;;;;;;;;;;;;;;;;;;
; 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

解决方案

The error is in $_FILES['userfile']['error']. You just have to check that this value is UPLOAD_ERR_INI_SIZE to detect if the file is bigger than the max size defined in your php.ini.


Resources :

这篇关于PHP文件上传大于upload_max_filesize和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆