为什么这个 Perl CGI 脚本无法正确上传图像?

发布于 2024-08-21 16:05:35 字数 803 浏览 3 评论 0原文

我有这样的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦爱 2024-08-28 16:05:35

您需要将

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.

夏尔 2024-08-28 16:05:35

我弄清楚了原因:正如 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.

生生漫 2024-08-28 16:05:35

添加了此“ENCTYPE='multipart/form-data'”而不是编码。

> <form action="/post.pl" method="post"  ENCTYPE='multipart/form-data'>

它起作用了

added this "ENCTYPE='multipart/form-data'" instead of encoding.

> <form action="/post.pl" method="post"  ENCTYPE='multipart/form-data'>

and it worked

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文