动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   Cgi/Perl → 阅读文章

 用 perl 实现文件上传

作者来源: 
阅读 1928 人次 , 2006-3-29 4:02:00 


示例的 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";

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:perl 域名查询程序
· 下一篇:用Perl语句来代替常用的操作系统命令
· perl写CGI时出现500号错误(Internal Server Error)原因总结
· Cgi入门教程之:13 留言板CGI代码
· Cgi入门教程之:12 代码详解
· Cgi入门教程之:4 使用Emacs
· 设置IIS以使用CGI


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077