简体   繁体   English

如何在perl中上传多个文件?

[英]How to upload multiple files in perl?

I need to upload multiple files using perl cgi.我需要使用 perl cgi 上传多个文件。 i used form type as我使用表单类型作为

enctype="multipart/form-data and also set multiple='multiple' in input type file. enctype="multipart/form-data 并在输入类型文件中设置 multiple='multiple'。

just need to know what should we do write at server side ?只需要知道我们应该在服务器端写什么?

Can anybody tell me how to upload multiple files using perl?谁能告诉我如何使用perl上传多个文件?

On the server side, you first retrive the file file handle like this:在服务器端,您首先像这样检索文件文件句柄:

 use CGI;
 my $q = CGI->new();
 my $myfh = $q->upload('field_name');

Now you have a filehandle to the temporary storage whither the file was uploaded.现在您有一个文件句柄到文件上传的临时存储。

The uploaded file anme can be had using the param() method.可以使用 param() 方法获得上传的文件名称。

 $filename = $q->param('field_name');

and the temporary file can be directly accessed via:并且可以通过以下方式直接访问临时文件:

 $filename = $query->param('uploaded_file');
 $tmpfilename = $query->tmpFileName($filename);

I highly recommend giving the CGI.pm docs a good solid read, a couple of times.我强烈建议给CGI.pm文档好好读几遍。 While not trivial, it's all rather straightforward.虽然不是微不足道,但一切都相当简单。

Something like this should handle multiple files upload:这样的事情应该处理多个文件上传:

my @fhs = $Cgi->upload('files');

foreach my $fh (@fhs) {
    if (defined $fh) {      
        my $type = $Cgi->uploadInfo($fh)->{'Content-Type'};
        next unless ($type eq "image/jpeg");

        my $io_handle = $fh->handle;
        open (OUTFILE,'>>','/var/directory/'.$fh);
        while (my $bytesread = $io_handle->read(my $buffer, 1024)) {
            print OUTFILE $buffer;
        }
        close (OUTFILE);
    }
}

Ofc 'files' is the name of the file upload form. Ofc 'files' 是文件上传表单的名称。

The following piece of code is enough and upload files present in the params to /storage location:以下代码就足够了,并将参数中存在的文件上传到 /storage 位置:

use CGI;
my $cgi = new CGI;
my @files = $cgi->param('multi_files[]');
my @io_handles=$cgi->upload('multi_files[]');
foreach my $upload(@files){
    print "Filename: $upload<br>";
    my $file_temp_path = "/storage";
    my $upload_file  = shift @io_handles; 
    open (UPLOADFILE,">$file_temp_path/$upload") or print "File Open Error";
    binmode UPLOADFILE;
    while (<$upload_file>) {
        print UPLOADFILE;
    }
}
print "Files Upload done";

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

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