欢迎光临
我们一直在努力

用 perl 实现文件上传-CGI教程,CGI文档

建站超值云服务器,限时71元/月

示例的 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)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用 perl 实现文件上传-CGI教程,CGI文档
分享到: 更多 (0)