使用Perl CGI上传文件

rur96b6h  于 2023-11-22  发布在  Perl
关注(0)|答案(1)|浏览(293)

我可以创建我的目录,但我似乎不能把文件放在目录中。

  1. #!/usr/bin/perl
  2. use Cwd;
  3. use CGI;
  4. my $dir = getcwd();
  5. print "Current Working Directory: $ dir\n";
  6. my $photoDir = "$dir/MyPhotos";
  7. mkdir $photoDir
  8. or die "Cannot mkdir $photoDir: $!"
  9. unless -d $photoDir;
  10. my $query = new CGI;
  11. my $filename = $query->param("Photo");
  12. my $description = $query->param("description");
  13. print "Current filename: $filename\n";
  14. my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); $filename = $name . $extension;
  15. print $filename;
  16. my $upload_filehandle = $query->upload("Photo");
  17. open ( UPLOADFILE, ">$photoDir/$filename" )
  18. or die "$!";
  19. binmode UPLOADFILE;
  20. while ( <$upload_filehandle> )
  21. { print UPLOADFILE; }
  22. close UPLOADFILE;

字符串
CGI堆栈跟踪显示没有错误,但日志显示没有输出

  1. LOG: 5 5020-0:0:0:0:0:0:0:1%0-9: CGI output 0 bytes.

bogh5gae

bogh5gae1#

**更新:**此答案已过时,今天您将直接获得IO::File兼容句柄:

  1. # undef may be returned if it's not a valid file handle
  2. if ( my $io_handle = $q->upload('field_name') ) {
  3. open ( my $out_file,'>>','/usr/local/web/users/feedback' );
  4. while ( my $bytesread = $io_handle->read($buffer,1024) ) {
  5. print $out_file $buffer;
  6. }
  7. }

字符串
请参阅更新的文档。

  • 原始答案:*

CGI.pm手册建议使用此路径保存上传的文件。请尝试此附加的检查和写入方法,看看是否有帮助。

  1. $lightweight_fh = $q->upload('field_name');
  2. # undef may be returned if it's not a valid file handle
  3. if (defined $lightweight_fh) {
  4. # Upgrade the handle to one compatible with IO::Handle:
  5. my $io_handle = $lightweight_fh->handle;
  6. open (OUTFILE,'>>','/usr/local/web/users/feedback');
  7. while ($bytesread = $io_handle->read($buffer,1024)) {
  8. print OUTFILE $buffer;
  9. }
  10. }


另外,请确保您的HTML表单具有如下所需的类型:<form action=... method=post enctype="multipart/form-data">

展开查看全部

相关问题