【问题标题】:Perl/CGI script fails on large uploadsPerl/CGI 脚本在大型上传时失败
【发布时间】:2012-01-03 12:06:35
【问题描述】:

我有这个 Perl/CGI 来上传文件并在上传时获取上传的文件大小。

该脚本适用于 500MB 以下的文件,但缓冲区 (OUTFILE) 在大约 500MB 后停止写入文件。 这是部分代码:

$u_size = $ENV{'CONTENT_LENGTH'};
if ($u_size > $max_size) {send_error ("Upload too big.  Maximum size is $max_size bytes and your file is $u_size bytes.");}

print_progress(0);
# Set up uploading function
$query = CGI->new(\&hook);

#define functions
sub hook  {
    if ($error) {return;}
    if (time >= $next_print) {
        $next_print = time + $delay;
        my ($filename, $buffer, $bytes_read, $data) = @_;
        if ($check_mime) {
            $filename =~ m/\.([^\.]+)$/;
            $ext = lc($1);
            print $ext;
            $check_mime = 0;
        }
        $percent = $bytes_read / $u_size;
        $filename =~ m/\\([^\\]+)$/;
        $filename = $1;
        print_progress($percent, $u_size, $bytes_read, $filename);
    }
}

sub print_progress {
    open(PROG, '>'.$uploaded_file_progress);
    print PROG '{"percent" : ' . ($_[0] * 100) . ', "total" : ' . $_[1] . ', "uploaded" : ' . $_[2] . ', "filename" : "' . $_[3] . '"}';
    close PROG;
}

#############

$uphandle = $query->upload($query->param());
binmode $uphandle;

if (!$error) {
    open OUTFILE, ">" . $uploaded_file;
    binmode OUTFILE;
    while($bytesread = read $uphandle, $buffer, 1024) {
      print OUTFILE $buffer;
    }
    #while (<$uphandle>) {print OUTFILE $_;}
    close OUTFILE;
}

如果脚本没有问题,我还需要检查哪些其他内容? 谢谢。

编辑:我在日志中有这个:超时等待 CGI 脚本的输出。 我该如何摆脱这个?我在 Google 上找不到明确的答案。

【问题讨论】:

    标签: perl upload cgi large-files


    【解决方案1】:

    您可能希望将 $CGI::POSTMAX 设置为大于 500MB 以查看是否可以解决问题。

    如果我没记错的话,500MB 是 POSTMAX 的默认值。我总是将我的设置为 2GB(但后来我用它来上传很多视频!)

    【讨论】:

      【解决方案2】:

      有一个 Apache httpd.conf 'MaxRequestLen' 设置值得研究(如果您使用的是 Apache)。

      【讨论】:

        【解决方案3】:

        我想你需要在 apache conf 中戳你的 apache TimeOut 配置变量。

        看到您正在使用 perl,这已经在 perlmonks.org 上提出了几次,并且弹出了这个链接作为回应。

        http://www.stonehenge.com/merlyn/LinuxMag/col39.html

        【讨论】:

        • 感谢TimeOut 配置的指导。这是两台服务器上唯一不同的地方。我不敢相信事情就这么简单。
        猜你喜欢
        • 2011-02-27
        • 2013-10-26
        • 2012-05-31
        • 1970-01-01
        • 2013-07-20
        • 2012-10-03
        • 1970-01-01
        • 2015-05-27
        • 2012-03-10
        相关资源
        最近更新 更多