【问题标题】:Perl "undefined value as a HASH reference" errorPerl“未定义的值作为 HASH 引用”错误
【发布时间】:2011-07-01 18:39:57
【问题描述】:

基于this article,我编写了一个perl 脚本来允许将文档文件上传到我的站点。但我得到“不能在upload.cgi 第38 行使用未定义的值作为HASH 引用”。 (第 38 行是 unless 子句上方的行)。 作为参考,我的网络主机安装了 Perl 5.8.8。

#!/usr/bin/perl -wT  

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

$CGI::POST_MAX = 1024 *1024 * 50;  #Max file size 50MB
my $safe_filename_characters = "a-zA-Z0-9_.-";  
my $upload_dir = "/home/antrobus/Originals";  

my $query = new CGI;  
my $filename = $query->param("manuscript");  

if ( !$filename )  
{  
 print $query->header ( );  
 print "The file is too large. Please email me to arrange other means of providing your manuscript.";  
 exit;  
}  

my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );  
$filename = $name . $extension;  
$filename =~ tr/ /_/;  
$filename =~ s/[^$safe_filename_characters]//g;  

if ( $filename =~ /^([$safe_filename_characters]+)$/ )  
{  
 $filename = $1;  
}  
else  
{  
 die "Filename contains invalid characters";  
}  

my @allowedtypes = qw(application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document application/vnd.oasis.opendocument.text application/rtf);
my $type = uploadInfo($filename)->{'Content-Type'};
unless ($type = @allowedtypes)
{  
 print $query->header ( );  
 print "Filetype not allowed. Please upload doc, docx, odt, or rtf files only.";  
 exit;  
}  

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

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

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

close UPLOADFILE;  

print $query->header ( );  
print qq~;  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  
 <head>  
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
   <title>File Uploaded!</title>  
   <style type="text/css">  
     img {border: none;}  
   </style>  
 </head>  
 <body>  
   <p>Thanks for uploading your manuscript!</p>  
 </body>  
</html>  
~;

【问题讨论】:

  • $type=@allowedtypes 表达式看起来很可疑。会期待像unless($allowedtypes{$type}) 或(5.10)unless($type~~@allowedtypes) 这样的东西。
  • 更改为 'unless($allowedtypes{$type})' 会产生以下错误:“全局符号“%allowedtypes”需要在 upload.cgi 第 38 行显示包名。”

标签: perl file-upload cgi


【解决方案1】:

如果您想拥有 MIME 内容类型

这样做,

$type = $query-&gt;uploadInfo($filename)-&gt;{'Content-Type'}; #CGI 对象丢失了

请参阅CGI 了解更多详情, 通常来自 CGI

$filename = $q->param('uploaded_file');
$type = $q->uploadInfo($filename)->{'Content-Type'};
unless ($type eq 'text/html') {
        die "HTML FILES ONLY!";
}

【讨论】:

  • 添加 '$q->' 或 '$query->" 没有效果。我写了我的代码 sn-p 粘贴在那个 CGI 文档上。我删除了 $query 对象,因为它最初是生成的未定义的子程序错误
【解决方案2】:

找到答案here。显然,污点检查混乱会破坏检查 mime 内容类型的能力。在污点检查和使用另一个变量之前将$filename 复制到另一个变量是可行的...接受它接受任何文件类型,而不仅仅是四个接受的文件类型(但这是另一个问题!)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 2013-02-16
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    相关资源
    最近更新 更多