使用AJAX的文件上传不能上传5MB文件 [英] Can't upload 5mb file using AJAX file uploader

查看:150
本文介绍了使用AJAX的文件上传不能上传5MB文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的文件上传插件(来源: https://github.com/valums/file-上传)将文件上传到我的网站。

I am using the file uploader plugin (from: https://github.com/valums/file-uploader) to upload files to my website.

如果您使用的是MODEN网络浏览器(如Firefox 6或Chrome 13),然后将其上传通过流在POST体文件,并且可以给你一个进度条。如果你使用IE浏览器(或旧的浏览器),它倒在标准$ _FILES(使用隐藏的iFrame)。

If you are using a moden web browser (like Firefox 6 or Chrome 13), then it uploads by streaming the file in the POST body, and can give you a progress bar. If you're using IE (or an old browser), it falls back on the standard $_FILES (using a hidden iFrame).

一切工作正常,但突然我不能上传5MB的文件在Chrome或Firefox浏览器。当我上传丁目或Firefox一个5MB的文件,我得到一个500错误,我的PHP code是从来没有跑。如果我使用Internet Explorer(使用$ _FILES),它工作正常。

Everything was working fine, but suddenly I can't upload 5MB files in Chrome or Firefox. When I upload a 5MB file in Chome or Firefox I get a 500 error and my PHP code is never even ran. If I use Internet Explorer (which uses $_FILES), it works fine.

这已经是一个配置的问题,因为我的PHP code从来没有运行。所以,我检查我的设置。

This has to be a configuration problem, as my PHP code never even runs. So, I checked my settings.

/etc/php.ini中

upload_max_filesize = 15M
post_max_size = 16M

我看了 LimitRequestBody ,但这是无处可寻(默认值是无限的)。

I looked for LimitRequestBody, but that's nowhere to be found (and the default is unlimited).

设置正确。我调试这一段时间,我无法弄清楚什么是错的。

Settings look right. I debugged this for a while, and I can not figure out what is wrong.

有我缺少一个设置?服务器已安装了Suhosin,如果该事项。

Is there a setting I'm missing? The server has suhosin installed, if that matters.

下面是后端(我用codeIgniter)code我使用。

Here is the backend (I'm using CodeIgniter) code I'm using.

// Can we use the fancy file uploader?
if($this->input->get('qqfile') !== FALSE){ // Yes we can :-)
    $name = preg_replace('/[^\-\(\)\d\w\.]/','_', $this->input->get('qqfile'));
    // Upload the file using black magic :-)
    $input = fopen("php://input", 'r');
    $temp = tmpfile();
    $fileSize = stream_copy_to_stream($input, $temp);
    fclose($input);
    if($fileSize > 15728640){
        $ret['error'] = 'File not uploaded: file cannot be larger than 15 MB';
    }               
    elseif(isset($_SERVER['CONTENT_LENGTH']) && $fileSize === (int)$_SERVER['CONTENT_LENGTH']){
        $path = $folder.'/'.$name; // Where to put the file
        // Put the temp uploaded file into the correct spot
        $target = fopen($path, 'w');
        fseek($temp, 0, SEEK_SET);
        stream_copy_to_stream($temp, $target);
        fclose($target);
        fclose($temp);

        $ret['fileSize'] = $fileSize;
        $ret['success'] = true;
    }
    else{
        $ret['error'] = 'File not uploaded: content length error';
    }
}
else{ // IE 6-8 can't use the fancy uploader, so use the standard $_FILES
    $file = $_FILES['qqfile'];
    $file['name'] = preg_replace('/[^\-\(\)\d\w\.]/','_', $file['name']);
    $config['file_name'] = $file['name'];
    // Upload the file using CodeIgniter's upload class (using $_FILES)
    $_FILES['userfile'] = $_FILES['qqfile'];
    unset($_FILES['qqfile']);
    $config['upload_path'] = $folder;
    $config['allowed_types'] = '*';
    $config['max_size'] = 15360; //15 MB
    $this->load->library('upload', $config);
    if($this->upload->do_upload()){ // Upload was successful :-)
        $upload = $this->upload->data();
        $ret['success'] = true;
        $ret['fileSize'] = $upload['fileSize']/1000;
    }
    else{ // Upload was NOT successful
        $ret['error'] = 'File not uploaded: '.$this->upload->display_errors('', '');
        $ret['type'] = $_FILES['userfile']['type'];
    }
    echo json_encode($ret);
}

我知道我的code ++工程,如文件大于4MB上传以下罚款(在所有浏览器)。我只是有文件大于5MB(使用Chrome /火狐)的一个问题。奇怪的是,这工作正常我的测试服务器上,但不是我的生产服务器。他们可能有不同的设置(了Suhosin是对生产,但不能在检验)。

I know my code works, as files less than 4MB upload fine (on all browsers). I only have a problem with files bigger than 5mb (using Chrome/Firefox). The weird thing is, this works fine on my test server, but not my production server. They probably have different settings (suhosin is on production, but not on test).

推荐答案

我看着我的Apache日志,发现

I looked in my apache logs, and found

PHP的致命错误:16777216字节用尽允许的内存大小(试图分配5242881字节)

PHP Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 5242881 bytes)

我改变了 memory_limit的为64M,现在似乎是确定。

I changed memory_limit to 64M, now it seems to be ok.

这篇关于使用AJAX的文件上传不能上传5MB文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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