用perl作的ftp(转)[@more@]
#!/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__>
Error: Bad Directory
is invalid. This problem is caused by one of the three following reasons: 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 /home/username/public_html/uploads The directory isn't writable. Make sure that this directory is writable by all users. At your UNIX command prompt, type chmod 777 $SAVE_DIRECTORY The directory you specified isn't really a directory. Make sure that this is indeed a directory and not a file. __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__> Error: Filename Problem
Bad Directory
The directory you specified:$SAVE_DIRECTORY = "$SAVE_DIRECTORY";
is invalid. This problem is caused by one of the three following reasons: 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 /home/username/public_html/uploads The directory isn't writable. Make sure that this directory is writable by all users. At your UNIX command prompt, type chmod 777 $SAVE_DIRECTORY The directory you specified isn't really a directory. Make sure that this is indeed a directory and not a file. __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__> Error: Filename Problem
Filename Problem
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 ' index'. Please rename the file on your computer, and try uploading it again. __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__> Error: Filename ProblemFilename Problem
You attempted to upload a file that isn't properly formatted. The file in question is $FILENAME_IN_QUESTION 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. __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__> Error: Limit ReachedLimit Reached
You have reached your upload limit. You attempted to upload $FILES_UPLOADED files, totalling $TOTAL_BYTES. This exceeds the maximum limit of $MAXIMUM_UPLOAD bytes, set by the system administrator. None of your files were successfully saved. Please try again. __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__> Upload FinishedUpload Finished
You uploaded $FILES_UPLOADED files totalling $TOTAL_BYTES total bytes. Individual file information is listed below: __END_OF_HTML_CODE__ foreach $key (keys (%Confirmation)) { print "$key - $Confirmation{$key} bytesn"; } print <<__end_of_html_code__> Thank you for using the File Upload! system. __END_OF_HTML_CODE__ exit; }来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8225414/viewspace-942991/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8225414/viewspace-942991/