2013-08-31 87 views
1

我在Perl中创建了一个将文件上传到服务器的CGI脚本。该脚本完美的作品:通过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, $_; 它可能是由于文件的标题无法识别而引起的。

对于如何在不保存文件的情况下存储文件内容有什么想法吗?

非常感谢您的回复。

回答

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); 
+0

非常感谢,它的工作。 – Mike