FTP 類
CodeIgniter的FTP類允許你將本地文件傳輸到遠程服務器上,同時可以移動、重命名和刪除遠程服務器上的文件。這個FTP類所包含的一個"mirroring"函數允許你通過FTP在遠程服務器上創(chuàng)建一個本地文件夾的鏡像。
注意:?不支持 SFTP 和 SSL FTP 協議, 僅支持標準 FTP 協議.
初始化類
像大多數其他CodeIgniter 類一樣,FTP 類在控制器里使用$this->load->library 函數來初始化:
$this->load->library('ftp');
一旦加載, FTP對象就可以使用: $this->ftp
使用例子
在這個例子中,首先建立一個到FTP服務器的連接,接著讀取一個本地文件然后以ASCII模式上傳。文件的權限被設置為755。
$this->load->library('ftp');
$config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE;
$this->ftp->connect($config);
$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);
$this->ftp->close();
下面的例子從FTP服務器上獲得了文件列表
$this->load->library('ftp');
$config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE;
$this->ftp->connect($config);
$list = $this->ftp->list_files('/public_html/');
print_r($list);
$this->ftp->close();
下面的例子在FTP服務器上創(chuàng)建了一個本地文件夾的鏡像。
$this->load->library('ftp');
$config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE;
$this->ftp->connect($config);
$this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
$this->ftp->close();
函數參考
$this->ftp->connect()
連接并登錄到FTP服務器,通過向函數傳遞一個數組來設置連接參數,或者你可以把這些參數保存在一個配置文件中。
下面例子演示了如何手動設置參數:
$this->load->library('ftp');
$config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['port']???? = 21;
$config['passive']??= FALSE;
$config['debug']????= TRUE;
$this->ftp->connect($config);
在配置文件中設置參數
如果你更傾向把FTP參數設置保存在一個配置文件中,只需創(chuàng)建一個名為ftp.php的文件, 把 $config 數組添加到該文件中,然后保存成config/ftp.php 它就會自動被讀取。
可用連接選項:
- hostname - FTP主機名。 通常看起來是這樣的:? ftp.example.com
- username - FTP用戶名。
- password - FTP密碼。
- port - 端口號。 默認設置為21
- debug - TRUE/FALSE (布爾值). 是否開啟調試顯示錯誤信息。
- passive - TRUE/FALSE (布爾值). 是否使用被動模式,默認設置為被動模式。
$this->ftp->upload()
將一個文件上傳到你的服務器上。本地路徑和遠程路徑這兩個參數是必需的,而傳輸模式和權限設置這兩個參數則是可選的。例如:
$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);
傳輸模式包括:? ascii, binary, 以及 auto (默認值)。如果使用了auto模式,將根據源文件的擴展名來自動選擇傳輸模式。
設置權限,你可以將一個octal (八進制)的權限值通過第四個參數傳遞過去。
$this->ftp->download()
從你的服務器下載文件。你必須提供遠程路徑和本地路徑,設置什么模式是可選的。例:
$this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');
Mode options are:? ascii, binary, and auto (the default). If auto is used it will base the mode on the file extension of the source file.
如果下載失敗返回 FALSE (包括 PHP 沒有寫入文件的權限的情況)
$this->ftp->rename()
作用是給一個文件重命名。請給出原文件名/路徑和新的文件名/路徑。
// 將文件 green.html 重命名為 blue.html
$this->ftp->rename('/public_html/foo/green.html', '/public_html/foo/blue.html');
$this->ftp->move()
作用是移動一個文件。請給出源路徑和目標路徑:
// 將文件 blog.html 從 "joe" 移動到 "fred"
$this->ftp->move('/public_html/joe/blog.html', '/public_html/fred/blog.html');
說明: 如果源文件名與目標文件名不同,文件將會被重命名。
$this->ftp->delete_file()
作用是刪除一個文件。請給出源文件名和所在路徑。
$this->ftp->delete_file('/public_html/joe/blog.html');
$this->ftp->delete_dir()
作用是刪除一個目錄以及此目錄下的全部內容。請給出原目錄的路徑,并以斜線結束。
重要提示? 請 非常謹慎 地使用這個函數。它將刪除你給出的目錄下的 所有內容,也就是說此目錄下的所有子目錄以及所有文件都會被刪除。請確保你給出的路徑是絕對正確的??梢韵仍囍褂?list_files() 函數來驗證你的路徑是否正確。
$this->ftp->delete_dir('/public_html/path/to/folder/');
$this->ftp->list_files()
允許你檢索你服務器上所有文件的列表,以數組的形式返回檢索結果。你必須給出要檢索的路徑。
$list = $this->ftp->list_files('/public_html/');
print_r($list);
$this->ftp->mirror()
檢索一個本地目錄下的所有內容(包括子目錄和所有文件),并通過FTP為這個目錄創(chuàng)建一份鏡像。源路徑下的任何結構都會被創(chuàng)建到服務器上。你必須給出源路徑和目標路徑:
$this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
$this->ftp->mkdir()
允許你在服務器上創(chuàng)建一個目錄。請給出你想要創(chuàng)建的目錄的完整路徑,截止到你要創(chuàng)建的目錄名,并在末尾添加斜線結束。你還可以在第二個參數中傳遞一個八進制的權限值。
// 創(chuàng)建一個名為"bar"的目錄
$this->ftp->mkdir('/public_html/foo/bar/', DIR_WRITE_MODE);
$this->ftp->chmod()
允許你設置文件權限。請給出你要設置的文件或者目錄所在的路徑:
// 將 "bar" 的權限設置為 777
$this->ftp->chmod('/public_html/foo/bar/', DIR_WRITE_MODE);
$this->ftp->close();
關閉到服務器的連接。當你上傳完畢時,建議使用這個函數關閉連接。
?