用perl作的ftp(轉)

post0發表於2007-08-10
用perl作的ftp(轉)[@more@]

#!/usr/local/bin/perl

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

# I found this script at

# 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 = "~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

Bad Directory

The directory you specified:


$SAVE_DIRECTORY = "$SAVE_DIRECTORY";


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

  1. 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

  2. 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

  3. 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 Problem

Filename 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 --&gt 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 Reached

Limit 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 Finished

Upload 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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章