1

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

如果您使用的是现代网络浏览器(如 Firefox 6 或 Chrome 13),那么它会通过在 POST 正文中流式传输文件来上传,并且可以为您提供进度条。如果您使用的是 IE(或旧浏览器),它会使用标准的 $_FILES(使用隐藏的 iFrame)。

一切正常,但突然我无法在 Chrome 或 Firefox 中上传 5MB 的文件。当我在 Chome 或 Firefox 中上传一个 5MB 的文件时,我收到 500 错误,我的 PHP 代码甚至从未运行过。如果我使用 Internet Explorer(使用 $_FILES),它可以正常工作。

这一定是一个配置问题,因为我的 PHP 代码甚至从未运行过。所以,我检查了我的设置。

/etc/php.ini

upload_max_filesize = 15M
post_max_size = 16M

我找了LimitRequestBody,但找不到(默认是无限的)。

设置看起来不错。我调试了一段时间,我不知道出了什么问题。

有没有我缺少的设置?如果这很重要,服务器已经安装了 suhosin。

这是我正在使用的后端(我正在使用 CodeIgniter)代码。

// 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);
}

我知道我的代码可以正常工作,因为小于 4MB 的文件可以正常上传(在所有浏览器上)。我只对大于 5mb 的文件有问题(使用 Chrome/Firefox)。奇怪的是,这在我的测试服务器上运行良好,但不是我的生产服务器。他们可能有不同的设置(suhosin 正在生产中,但未在测试中)。

4

2 回答 2

3

请通过查看检查您的 php.ini 设置是否正确加载<?php phpinfo(); ?>

于 2011-09-21T14:14:16.500 回答
0

我查看了我的 apache 日志,发现

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

我改成memory_limit64M了,现在好像可以了。

于 2011-09-21T15:56:55.563 回答