用于文件上传的 Perl 脚本

标签 perl cgi

我正在尝试用 Perl 编写一个脚本,允许用户上传文件。目前,它说它正在工作,但实际上并没有上传文件!

这是代码:

 #!/usr/bin/perl
 use CGI;
 my $cgi = new CGI;
 my $dir = 'sub';
 my $file = $cgi->param('file');
 $file=~m/^.*(\\|\/)(.*)/;
 # strip the remote path and keep the filename
 my $name = $2;
 open(LOCAL, ">$dir/$name") or print 'error';
 while(<$file>) {
    print LOCAL $_;
 }
 print $cgi->header();
 print $dir/$name;
 print "$file has been successfully uploaded... thank you.\n";enter code here

最佳答案

正如 CanSpice 指出的那样,this question给出答案:

 #!/usr/bin/perl
 use CGI;
 my $cgi = new CGI;
 my $dir = 'sub';
 my $file = $cgi->param('file');
 $file=~m/^.*(\\|\/)(.*)/;
 # strip the remote path and keep the filename
 my $name = $2;
 open(LOCAL, ">$dir/$name") or print 'error';
 my $file_handle = $cgi->upload('file');     // get the handle, not just the filename
 while(<$file_handle>) {               // use that handle
    print LOCAL $_;
 }
 close($file_handle);                        // clean the mess
 close(LOCAL);                               // 
 print $cgi->header();
 print $dir/$name;
 print "$file has been successfully uploaded... thank you.\n";enter code here

关于用于文件上传的 Perl 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7770176/

相关文章:

postgresql - 无法使用 DBIx::Class::Schema::Versioned 创建非虚拟 View

linux - 代理必须指定为绝对URI; '192.168.3.10' 不在/usr/share/perl/5.26/CPAN/FTP.pm 第 355 行

perl - 如何限制 perl 进程输出文件的大小?

php - 如何获取我站点(我的服务器)用户的 MAC 地址

python - 使用 python cgi 发送 .png 文件

mysql - 将 CGI REMOTE_ADDR 转换为网络字节顺序以便与 MySql INET_NTOA 一起使用

python - 请在 Perl 或 Ruby 中引入多处理库

python - 输出正确的 CGI、Python 脚本时遇到问题?

Python CGI 源代码中没有 HTML 困惑?

Python os 模块用相对路径打开当前目录上方的文件