使用Perl CGI上传文件

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

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

#!/usr/bin/perl

use Cwd;
use CGI;

my $dir = getcwd();
print "Current Working Directory: $ dir\n";

my $photoDir = "$dir/MyPhotos";

mkdir $photoDir
        or die "Cannot mkdir $photoDir: $!"
        unless -d $photoDir;
        
        
my $query = new CGI;
my $filename = $query->param("Photo");
my $description = $query->param("description");

print "Current filename: $filename\n";

my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); $filename = $name . $extension;
print $filename;
my $upload_filehandle = $query->upload("Photo");


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

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

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

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

bogh5gae

bogh5gae1#

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

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

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

  • 原始答案:*

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

$lightweight_fh  = $q->upload('field_name');

     # undef may be returned if it's not a valid file handle
     if (defined $lightweight_fh) {
       # Upgrade the handle to one compatible with IO::Handle:
       my $io_handle = $lightweight_fh->handle;

       open (OUTFILE,'>>','/usr/local/web/users/feedback');
       while ($bytesread = $io_handle->read($buffer,1024)) {
         print OUTFILE $buffer;
       }
     }


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

相关问题