简体   繁体   English

使用 Perl CGI 上传文件

[英]Upload file using Perl CGI

I am able to create my directory but I cannot seem to place the file in the directory.我能够创建我的目录,但我似乎无法将文件放在目录中。

#!/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;

The CGI stack trace shows no errors but the log shows there is no output CGI 堆栈跟踪显示没有错误,但日志显示没有输出

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

CGI.pm manual suggests this path to saving uploaded files. CGI.pm 手册建议使用此路径来保存上传的文件。 Try this additional check and write method and see if it helps.试试这个额外的检查和写入方法,看看它是否有帮助。

     $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;
       }
     }

Also make sure you have your HTML form has required type like this: <form action=... method=post enctype="multipart/form-data">还要确保您的 HTML 表单具有如下所需的类型: <form action=... method=post enctype="multipart/form-data">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM