2012-10-08 110 views
0

我有下面的PERL脚本试图下载包含该脚本输出的文件。通过PERL脚本下载文件

我面对它说的错误:

位于C未定义的值无法调用 “头”:/Apache24/cgi-bin/trim.pl

但是我输出文件被正确写入正确的目的地。请帮助我解决问题并获取输出文件。

use strict; 
use CGI; 
use CGI::Carp qw (fatalsToBrowser); 
use File::Basename; 

my $q   = new CGI; 
my $upload_dir = "C:/Apache24/htdocs/files" ; 
my ($bytesread, $buffer); 
my $numbytes  = 1024; 
my $trim_out_dir = "C:/Users/Admin/Desktop/vijay/Metagenomics/NGSQCToolkit_v2.3/CGI_Out"; 
my $out_file  = "$trim_out_dir/trimmed.fasta"; 
my $safe_filename_characters = "a-zA-Z0-9_.-"; 

$q->cgi_error and error($q, "Error transferring file: " . $q->cgi_error); 

my $file   = $q->param("seq")  || error($q, "No file received."); 
my $length  = $q->param("len"); 
my $quality  = $q->param("qual"); 

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

while ($bytesread = read($file, $buffer, $numbytes)) { 
    print UPLOADFILE $buffer; 
} 

close UPLOADFILE; 
system("Reads.pl -i $upload_dir/$file -n $length -o $out_file"); 
open (DOWNLOADFILE, "<$out_file") or die "$!"; 

print $cgi->header(
    -type  => 'application/octet-stream' 
    -attachment => '$out_file'); 

close DOWNLOADFILE; 

回答

0

而不是

print $cgi->header(

你大概的意思

print $q->header(

使用use strict应该抓住这个错误。

3

这不会与strict编译,并且由于您使用的是strict,因此这似乎不是您执行的代码。您的$cgi对象从不定义,因此会在编译时停止脚本。这条线:

print $cgi->header(

也许应该

print $q->header(

你也应该知道,

-attachment => '$out_file'); 

将意味着attachment PARAM将不包含变量$out_file的价值,它会包含变量的文字名称。如果您希望它包含变量的值,则需要删除引号或使用双引号。

更重要的是,您还应该知道您的脚本非常不安全。您直接从cgi对象使用值而不验证它们。 open语句和system语句都可以轻松地在您的系统上执行任意命令。例如:

$out_file = "foo; rm -rf /"; 
system("Reads.pl -i $upload_dir/$file -n $length -o $out_file"); 

您不妨阅读documentation on security,并可能使用taint mode-T开关)。

+0

我已经调整了我的脚本如下。由于我是PERL的新手,我不了解文档安全概念。 open(DOWNLOADFILE,“<$ out_file”)或死“$!”; print $ q-> header( -type =>'application/octet-stream' -attachment =>“$ out_file”); 关闭DOWNLOADFILE; 这次脚本运行成功,但out_file不可用于下载。请你帮我解决这个问题。 – Vijay