为什么这个 Perl CGI 脚本无法正确上传图像?
我有这样的代码:
use CGI;
use File::Basename;
use Image::Magick;
use Time::HiRes qw(gettimeofday);
my $query = new CGI;
my $upload_dir = "../images"; #location on our server
my $filename=$query->param("img");
my $timestamp = int (gettimeofday * 1000);
my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
$filename = $name ."_".$timestamp. $extension;
#upload the image
my $upload_filehandle =$query->param("img");
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
然后我调整图像的大小。我的问题是图片没有上传。我有一个名为 $filename 的空文件。我省略了与调整大小相关的部分,但我仍然有代码。所以我确信文件没有正确上传。我缺少任何图书馆吗? 有什么帮助吗?请..
I have this code:
use CGI;
use File::Basename;
use Image::Magick;
use Time::HiRes qw(gettimeofday);
my $query = new CGI;
my $upload_dir = "../images"; #location on our server
my $filename=$query->param("img");
my $timestamp = int (gettimeofday * 1000);
my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
$filename = $name ."_".$timestamp. $extension;
#upload the image
my $upload_filehandle =$query->param("img");
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
Then I do resize for the image. My problem is that the image is not uploaded. I have an empty file having the name of $filename. I omitted the part related to resize and I still have the code. So I'm sure that the file is not being uploaded correctly. Am I missing any library?
Any help?Plz..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将
my $upload_filehandle =$query->param("img");
更改为
my $upload_filehandle =$query->upload("img");
>根据本教程。
希望有帮助。
You need to change
my $upload_filehandle =$query->param("img");
to
my $upload_filehandle =$query->upload("img");
according to this tutorial.
Hope it helps.
我弄清楚了原因:正如 CGI.pm 文档 中指出的,表单的编码类型必须为
multipart/form-data
。当我添加它时,一切都正常了。I figured out why: As pointed out in the CGI.pm docs, the form's encoding type must be
multipart/form-data
. When I added that, everything worked.添加了此“ENCTYPE='multipart/form-data'”而不是编码。
它起作用了
added this "ENCTYPE='multipart/form-data'" instead of encoding.
and it worked