簡體   English   中英

通過Perl CGI腳本上傳文件而不存儲文件

[英]upload file through Perl CGI script without storing file

我在Perl中創建了一個CGI腳本,該腳本將文件上傳到服務器。 該腳本可以完美運行:

my $upload_filehandle = $query->upload("config");

if (!$upload_filehandle)
{
    die "Configuration file could not be loaded";
}

open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
    print UPLOADFILE;
}

close UPLOADFILE;

問題是我不想將文件存儲到服務器上,而只想將其內容的一部分存儲在Perl腳本中的某些變量中。 我不知道該怎么做。 我嘗試了以下代碼:

my @array;
my $upload_filehandle = $query->upload("config");

if (!$upload_filehandle)
{
    die "Configuration file could not be loaded";
}

open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
    push @array, $_;
}


close UPLOADFILE;

我想將文件內容存儲在數組中,但它給出了一個錯誤:

[Sat Aug 31 18:03:27 2013] [error] [client 127.0.0.1] malformed header from script. Bad      header=\xff\xd8\xff\xe11\xdcExif: loadConfig.cgi, referer: http://localhost/cgi-bin/uploadFile.cgi

我認為這是在行push @array, $_; 可能是由於無法識別文件的標頭引起的。

您是否有關於如何在不保存文件的情況下存儲文件內容的想法?

預先感謝您的回復。

看起來您正在嘗試在正確的HTTP標頭之前打印文件( \\xff\\xd8\\xff\\xe11\\xdcExif ):

print $query->header;

我整理了一下您的腳本,效果很好:

#!/usr/bin/perl

use strict;
use warnings;

use CGI;
use CGI::Carp 'fatalsToBrowser';

my $query = CGI->new;

my $upload_filehandle = $query->upload("config")
    or die "Configuration file could not be loaded";


my @array = <$upload_filehandle>;

print $query->header;

use Data::Dumper;
print Dumper(\@array);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM