分享
 
 
 

用perl作的ftp

王朝perl·作者佚名  2008-05-18
窄屏简体版  字體: |||超大  

#!/usr/local/bin/perl

#################################################################

# I found this script at http://www.terminalp.com

# it was written by a guy named Jeff who sold the domain and

# disappeared. I deleted the original header to save space

# so I am distributing this as is. If Jeff sees this, please

# email me at dreun@eskimo.com!! Thanks, Ron Hagerman

#################################################################

BEGIN {

$SAVE_DIRECTORY = "/your/literal/path/incoming/";

#定义上载的文件的存放位置

$MAXIMUM_UPLOAD = 0;

#最大上载数量

$ALLOW_INDEX = 0;

是否允许上载文件名为index.*的文件

$SUCCESS_LOCATION = "http://www.yourserver.com/~you/page.html";

#定义当上载成功以后,显示该URL指向的内容

}

$| = 1;

#设定$OUTPUT_AUTOFLUSH为1,也就是使STDOUT不对输出进行缓冲,而是直接输出。

chop $SAVE_DIRECTORY if ($SAVE_DIRECTORY =~ //$/);

#若保存目的地址以"/"结尾 则将其删除

use CGI qw(:standard);

#使用CGI模块

$query = new CGI;

#生成一个新的CGI对象

#当指定的保存目的目录名不存在或不可写或不是目录时 输出错误信息

if ( (!(-e $SAVE_DIRECTORY)) ||

(!(-W $SAVE_DIRECTORY)) ||

(!(-d $SAVE_DIRECTORY)) ) {

print header;

#输出http信息头

print <<__END_OF_HTML_CODE__;

#这里的<<__END_OF_HTML_CODE__指示print语句输出文件后面的内容 直到遇到__END_OF_HTML_CODE__ 你可以使用别的符号来替代__END_OF_HTML_CODE__

<HTML>

<HEAD>

<TITLE>Error: Bad Directory</TITLE>

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<H1>Bad Directory</H1>

<P>

The directory you specified:

<BR>

<BLOCKQUOTE>

<TT>$SAVE_DIRECTORY = "<B>$SAVE_DIRECTORY</B>";</TT>

</BLOCKQUOTE>

<BR>

is invalid. This problem is caused by one of the three following reasons:

<OL>

<LI>The directory doesn't exist. Make sure that this directory is a complete path name, not

a URL or something similar. It should look similar to <TT>/home/username/public_html/uploads</TT>

<P>

<LI>The directory isn't writable. Make sure that this directory is writable by all users. At

your UNIX command prompt, type <TT>chmod 777 $SAVE_DIRECTORY</TT>

<P>

<LI>The directory you specified isn't really a directory. Make sure that this is indeed a directory

and not a file.

</OL>

</BODY>

</HTML>

__END_OF_HTML_CODE__

exit;

}

foreach $key (sort {$a <=> $b} $query->param()) {

#对html form中的各个元素的名字进行排序 然后对每个名字进行如下操作

next if ($key =~ /^s*$/);

#若名字为空 则进行下一次循环

next if ($query->param($key) =~ /^s*$/);

#若名字对应的值为空 则进行下一次循环

next if ($key !~ /^file-to-upload-(d+)$/);

#若名字不为file-to-upload-(数字)的形式 则进行下一次循环

$Number = $1;

if ($query->param($key) =~ /([^/\]+)$/) {

#若取到的上载路径中的文件名部分不为空则

$Filename = $1;

$Filename =~ s/^.+//;

#将取到的文件名保存到变量$Filename中,并且去除文件名前的"."符号

$File_Handle = $query->param($key);

#完全路径的文件名保存到$File_Handle;

if (!$ALLOW_INDEX && $Filename =~ /^index/i) {

#若不允许上载文件名为index.*的文件而文件却恰恰为index.*形式则输出错误信息

print header;

print <<__END_OF_HTML_CODE__;

<HTML>

<HEAD>

<TITLE>Error: Filename Problem</TITLE>

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<H1>Filename Problem</H1>

<P>

You attempted to upload a file that isn't properly formatted. The system administrator

has decided that you can't upload files that begin with the word '<B>index</B>'. Please

rename the file on your computer, and try uploading it again.

<P>

</BODY>

</HTML>

__END_OF_HTML_CODE__

exit;

}

#end of decide whether the file can be index.* style

} else {

#当取得的文件名为空 则输出错误信息

$FILENAME_IN_QUESTION = $query->param($key);

#取得该文件路径到变量$FILENAME_IN_QUESTION中 然后输出错误信息

print header;

print <<__END_OF_HTML_CODE__;

<HTML>

<HEAD>

<TITLE>Error: Filename Problem</TITLE>

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<H1>Filename Problem</H1>

<P>

You attempted to upload a file that isn't properly formatted. The file in question

is <TT><B>$FILENAME_IN_QUESTION</B></TT> Please rename the file on your computer, and

attempt to upload it again. Files may not have forward or backward slashes in their

names. Also, they may not be prefixed with one (or more) periods.

<P>

</BODY>

</HTML>

__END_OF_HTML_CODE__

exit;

}

if (!open(OUTFILE, ">$SAVE_DIRECTORY/$Filename")) {

#以写入的方式在上载目录下创建与上载文件同名的文件 若打开错误则输出错误信息

print "Content-type: text/plainnn";

print "-------------------------n";

print "Error:n";

print "-------------------------n";

print "File: $SAVE_DIRECTORY/$Filenamen";

print "-------------------------n";

print "There was an error opening the Output Filen";

print "for Writing.nn";

print "Make sure that the directory:n";

print "$SAVE_DIRECTORYn";

print "has been chmodded with the permissions '777'.nn";

print "Also, make sure that if your attemptingn";

print "to overwrite an existing file, that then";

print "existing file is chmodded '666' or better.nn";

print "The Error message below should help you diagnosen";

print "the problem.nn";

print "Error: $!n";

exit;

}

undef $BytesRead;

undef $Buffer;

while ($Bytes = read($File_Handle,$Buffer,1024)) {

#从要上载源文件读取1K的内容到变量buffer中 循环直到将文件内容全部读完

$BytesRead += $Bytes;

#更新从该文件读取的字节数总数

print OUTFILE $Buffer;

#输出$buffer内容到目的文件中

}

push(@Files_Written, "$SAVE_DIRECTORY/$Filename");

#将上载的带有本机路径文件名加入到数组@Files_Written中

$TOTAL_BYTES += $BytesRead;

#更新上载的数据量的总和

$Confirmation{$File_Handle} = $BytesRead;

#置关联数组值 {file_to_read --> file_to_read_length}

close($File_Handle);

close(OUTFILE);

#关闭两个文件

chmod (0666, "$SAVE_DIRECTORY/$Filename");

#修改上载的文件访问权限位为666

}

$FILES_UPLOADED = scalar(keys(%Confirmation));

#取得上载的文件数目

if ($TOTAL_BYTES > $MAXIMUM_UPLOAD && $MAXIMUM_UPLOAD > 0) {

#若上载的所有文件的大小总和大于变量$MAXIMUM_UPLOAD中规定的最大上载文件大小

#且$MAXIMUM_UPLOAD不为0 则删除上载的文件

foreach $File (@Files_Written) {

unlink $File;

}

#打印错误信息

print header;

print <<__END_OF_HTML_CODE__;

<HTML>

<HEAD>

<TITLE>Error: Limit Reached</TITLE>

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<H1>Limit Reached</H1>

<P>

You have reached your upload limit. You attempted to upload <B>$FILES_UPLOADED</B> files, totalling

<B>$TOTAL_BYTES</B>. This exceeds the maximum limit of <B>$MAXIMUM_UPLOAD</B> bytes, set by the system

administrator. <B>None</B> of your files were successfully saved. Please try again.

<P>

</BODY>

</HTML>

__END_OF_HTML_CODE__

exit;

}

if ($SUCCESS_LOCATION !~ /^s*$/) {

print $query->redirect($SUCCESS_LOCATION);

#若定义的$SUCCESS_LOCATION不为空 则显示$SUCCESS_LOCATION指定的页面

#否则输出信息

} else {

print header;

print <<__END_OF_HTML_CODE__;

<HTML>

<HEAD>

<TITLE>Upload Finished</TITLE>

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<H1>Upload Finished</H1>

<P>

You uploaded <B>$FILES_UPLOADED</B> files totalling <B>$TOTAL_BYTES</B> total bytes. Individual

file information is listed below:

<PRE>

__END_OF_HTML_CODE__

foreach $key (keys (%Confirmation)) {

print "$key - $Confirmation{$key} bytesn";

}

print <<__END_OF_HTML_CODE__;

</PRE>

<P>

Thank you for using the File Upload! system.

<P>

</BODY>

</HTML>

__END_OF_HTML_CODE__

exit;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有