用perl作的ftp(轉)
用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
is invalid. This problem is caused by one of the three following reasons:Error: Filename Problem Error: Filename Problem Error: Limit Reached Upload Finished
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, nota 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. Atyour UNIX command prompt, type chmod 777 $SAVE_DIRECTORY
- The directory you specified isn't really a directory. Make sure that this is indeed a directoryand not a file.
Filename Problem
You attempted to upload a file that isn't properly formatted. The system administratorhas decided that you can't upload files that begin with the word 'index'. Pleaserename 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__>Filename Problem
You attempted to upload a file that isn't properly formatted. The file in questionis $FILENAME_IN_QUESTION Please rename the file on your computer, andattempt to upload it again. Files may not have forward or backward slashes in theirnames. 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__>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 systemadministrator. 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
You uploaded $FILES_UPLOADED files totalling $TOTAL_BYTES total bytes. Individualfile 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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Perl中FTP的使用FTP
- Perl 作為命令列實用程式(轉)命令列
- 用perl訪問mysql資料庫(轉)MySql資料庫
- 一個用Perl分析Apache Log的簡單程式(轉)Apache
- 轉:用Perl實現單詞計數器
- Linux指令碼語言PERL 的模板應用分析(轉)Linux指令碼
- 用FTP自動傳送檔案(轉)FTP
- 比較perl模組的版本(轉)
- 用PHP即時新增,刪除FTP帳號和更改FTP密碼的函式 (轉)PHPFTP密碼函式
- 用Perl來管理Apache驗證身份時所用的密碼(轉)Apache密碼
- 很不錯的PERL管道應用
- perl橫豎轉換
- FTP的完整類<1><轉>FTP
- FTP的完整類<2><轉>FTP
- FTP的完整類<3><轉>FTP
- 在Linux系統下FTP的配置與應用(轉)LinuxFTP
- Cultured Perl: 吸引 C 和 Java 程式設計師目光的 Perl 5.6 (轉)Java程式設計師
- FTP工作原理(轉)FTP
- ftp,ftp是幹什麼的,怎麼運用呢?FTP
- 簡單實用的FTP客戶端:Viper FTP for MacFTP客戶端Mac
- 用自動ftp提高工作效率(轉)FTP
- perl除錯哲學(轉)除錯
- Perl 連線 SQL Server (轉)SQLServer
- 比較 python & perl(轉)Python
- perl script for scan TCP ports .(轉)TCP
- 今天上午寫的perl---用於鈴音檔名的轉換
- 實用的FTP客戶端:FTP Disk for Mac中文版FTP客戶端Mac
- 用java實現ftp的功能JavaFTP
- Perl 正則式應用
- 用perl寫的一個watchdog程式
- FTP命令詳解(轉)FTP
- FTP命令詳解 (轉)FTP
- 本地除錯PERL CGI程式(轉)除錯
- 測試mod_perl模組(轉)
- perl的hash轉json後按key排序JSON排序
- Perl的NT安裝實驗報告(轉)
- Perl程式與c程式速度的比較(轉)C程式
- perl Statistics::Descriptive Perl 的統計模組