【发布时间】:2017-05-23 22:38:43
【问题描述】:
我正在尝试将文件上传添加到现有网页。
每次我上传我都会得到一个损坏的文件。
我确保在文件句柄上设置了binmode。我还在表单中将输入 enctype 设置为 multipart/formdata。
我的代码如下
$article{upload_file_name} = $cgi->param( 'upFile' );
$article{upload_file} = $cgi->upload( 'upFile' );
if ( $article{upload_file_name} ne "" or $article{upload_file_name} ne null ) {
open( UPLOADS, ">$uploads_dir/$article{upload_file_name}" )
or die "Could not open $uploads_dir/$article{upload_file_name}";
binmode UPLOADS;
while ( <$article{upload_file}> ) {
print UPLOADS;
}
close UPLOADS;
}
我也试过了
$article{upload_file} = $cgi->param( 'upFile' );
if ( $article{upload_file} ne "" or $article{upload_file} ne null ) {
open( UPLOADS, ">$uploads_dir/$article{upload_file}" )
or die "Could not open $uploads_dir/$article{upload_file}";
binmode UPLOADS;
while ( <$article{upload_file}> ) {
print UPLOADS;
}
close UPLOADS;
}
【问题讨论】:
-
一方面,您需要在脚本顶部添加
use strict;和use warnings 'all';; Perl 中没有null,所以$article{upload_file_name} ne null检查文件名是否等于文本“null”。 -
您是否在某处定义了名称为
null的子例程或常量?你的f($article{upload_file_name} ne "" or $article{upload_file_name} ne null)应该是一个语法错误,除非你没有use strict。 Perl 中没有null。您想要undef,但您无法通过ne查询undef。事实上,做if ($article{upload_file_name}) { ... }就足够了,因为只要有一些不是空的或undef的值就会为真。 -
见我评论的第二部分。您只需要检查
$article{upload_file_name}是否是 truthy 值。0、undef和空字符串""为假。 Perl 中的其他一切都是正确的。 -
好的,感谢您提供有关 null 的信息,我没有意识到它不是通用的。然而,这不是错误的原因,因为我有一个损坏的文件,它显然符合我的声明。我确实对 null 进行了更改,并将更新我的 Q。cgi->upload 和 cgi->param 之间有区别吗?
标签: perl file-upload cgi filehandle