用 perl 实现文件上传

示例的 HTML 文件如下:
<html>
<body>
<form method="POST" action="psupload.cgi" ENCTYPE="multipart/form-data">
File 1:
<input type="file" name="FILE1">
<br>
File 2:
<input type="file" name="FILE2">
<br>
<input type="submit" value="Upload!">
</form>
</body>
</html>


后台的 Perl 程序如下:

#!/usr/bin/perl
#######################################
## Perl Services Upload Helper v1.0 ##
## http://www.perlservices.com ##
## perlservices@perlservices.com ##
## ###########################################
## You should carefully read all of the following terms and conditions ##
## before using this program. Your use of this software indicates your ##
## acceptance of this license agreement and warranty. ##
## This program is being distributed as freeware. It may be used ##
## free of charge, but not modified below the line specified. This copyright ##
## must remain intact. ##
## ##
## By using this program you agree to indemnify Perl Services from any ##
## liability. ##
## ##
## Selling the code for this program without prior written consent is ##
## expressly forbidden. Obtain permission before redistributing this ##
## program over the Internet or in any other medium. In all cases the ##
## copyright must remain intact. ##
## ##
## There are security hazards involved with this script. Read the readme file##
## before using the script. ##
################################################################################

##
## Start setting up options here:

## Your path to where you want your files uploaded.
## Note: NO trailing slash
$basedir = "/home/path/to/directory";

## Do you wish to allow all file types? yes/no (no capital letters)
$allowall = "yes";

## If the above = "no"; then which is the only extention to allow?
## Remember to have the LAST 4 characters i.e. .ext
$theext = ".gif";

## The page you wish it to forward to when done:
## I.E. http://www.mydomainname.com/thankyou.html
$donepage = "http://www.perlservices.com/";


################################################
################################################
## DO NOT EDIT OR COPY BELOW THIS LINE ##
################################################
################################################

use CGI;
$onnum = 1;

while ($onnum != 11) {
my $req = new CGI;
my $file = $req->param("FILE$onnum");
if ($file ne "") {
my $fileName = $file;
$fileName =~ s!^.*(//|//)!!;
$newmain = $fileName;
if ($allowall ne "yes") {
if (lc(substr($newmain,length($newmain) - 4,4)) ne $theext){
$filenotgood = "yes";
}
}
if ($filenotgood ne "yes") {
open (OUTFILE, ">$basedir/$fileName");
print "$basedir/$fileName<br>";
while (my $bytesread = read($file, my $buffer, 1024)) {
print OUTFILE $buffer;
}
close (OUTFILE);
}
}
$onnum++;
}

print "Content-type: text/html/n";
print "Location:$donepage/n/n";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值