我在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, $_;
它可能是因为文件的标题无法识别而引起的。
您对如何在不保存文件的情况下存储文件内容有任何想法吗?
提前感谢您的回复。
答案 0 :(得分:1)
看起来您正在尝试在正确的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);