?
This document uses PHP Chinese website manual Release
CodeIgniter的FTP類允許文件傳輸到遠程服務器。遠程文件也可以被移動,重命名和刪除。FTP類還包含一個“鏡像”功能,允許通過FTP遠程重建整個本地目錄。
注意
不支持SFTP和SSL FTP協(xié)議,只有標準FTP。
使用FTP類
初始化類
用法示例
類參考
像CodeIgniter中的大多數其他類一樣,F(xiàn)TP類在您的控制器中使用$ this-> load-> library函數進行初始化:
$this->load->library('ftp');
一旦加載,F(xiàn)TP對象將可用:$ 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();
在這個例子中,從服務器中檢索一個文件列表。
$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();
在這個例子中,本地目錄在服務器上被鏡像。
$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();
class CI_FTPconnect([$config = array()])
參數: | $ config(array) - 連接值 |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ config(array) - 連接值
Returns: TRUE on success, FALSE on failure
Return type: bool
連接并登錄到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首選項
如果您愿意,您可以將您的FTP首選項存儲在配置文件中。只需創(chuàng)建一個名為ftp.php的新文件,在該文件中添加$ config數組。然后將文件保存在application / config / ftp.php文件中,它將自動使用。
可用的連接選項
選項名稱默認值說明**主機名**不適用FTP主機名(通常類似于:ftp.example.com)**用戶名**不適用FTP用戶名**密碼**不適用FTP密碼**端口* * 21 FTP服務器端口號** debug ** FALSE TRUE / FALSE(boolean):是否啟用調試以顯示錯誤消息** passive ** TRUE TRUE / FALSE(布爾值):是否使用被動模式
upload($locpath, $rempath[, $mode = 'auto'[, $permissions = NULL]])
參數: | $ locpath(string) - 本地文件路徑$ rempath(string) - 遠程文件路徑$ mode(string) - FTP模式,默認為'auto'(選項為'auto','binary','ascii')$ permissions (int) - 文件權限(八進制) |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ locpath(字符串) - 本地文件路徑
$ rempath(字符串) - 遠程文件路徑
$ mode(字符串) - FTP模式,默認為'auto'(選項為:'auto','binary','ascii')
$ permissions(int) - 文件權限(八進制)
返回:成功時為TRUE,失敗時為FALSE
Return type: bool
將文件上傳到您的服務器。您必須提供本地路徑和遠程路徑,并且可以選擇設置模式和權限。例:
$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);
如果使用“自動”模式,它將以源文件的文件擴展名為基礎。
如果設置,權限必須作為八進制值傳遞。
download($rempath, $locpath[, $mode = 'auto'])
參數: | $ rempath(string) - 遠程文件路徑$ locpath(string) - 本地文件路徑$ mode(string) - FTP模式,默認為'auto'(選項為'auto','binary','ascii') |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ rempath(字符串) - 遠程文件路徑
$ locpath(字符串) - 本地文件路徑
$ mode(字符串) - FTP模式,默認為'auto'(選項為:'auto','binary','ascii')
返回:成功時為TRUE,失敗時為FALSE
Return type: bool
從服務器下載文件。您必須提供遠程路徑和本地路徑,您可以選擇設置模式。例:
$this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');
如果使用“自動”模式,它將以源文件的文件擴展名為基礎。
如果下載未成功執(zhí)行(包括PHP沒有寫入本地文件的權限),則返回FALSE。
rename($old_file, $new_file[, $move = FALSE])
參數: | $ old_file(字符串) - 舊文件名$ new_file(字符串) - 新文件名$ move(bool) - 是否正在執(zhí)行移動 |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ old_file(字符串) - 舊文件名
$ new_file(字符串) - 新文件名
$ move(bool) - 是否正在執(zhí)行移動
Returns: TRUE on success, FALSE on failure
Return type: bool
允許您重命名文件。提供源文件名/路徑和新文件名/路徑。
//將green.html重命名為blue.html $ this-> ftp-> rename('/ public_html / foo / green.html','/public_html/foo/blue.html');
move($old_file, $new_file)
參數: | $ old_file(字符串) - 舊文件名$ new_file(字符串) - 新文件名 |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ old_file(字符串) - 舊文件名
$ new_file(字符串) - 新文件名
Returns: TRUE on success, FALSE on failure
Return type: bool
讓你移動一個文件。提供源和目標路徑:
// Moves blog.html from "joe" to "fred" $this->ftp->move('/public_html/joe/blog.html', '/public_html/fred/blog.html');
注意
如果目標文件名不同,則文件將被重命名。
delete_file($filepath)
參數: | $ filepath(字符串) - 要刪除的文件的路徑 |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ filepath(字符串) - 要刪除的文件的路徑
Returns: TRUE on success, FALSE on failure
Return type: bool
讓你刪除一個文件。使用文件名提供源路徑。
$this->ftp->delete_file('/public_html/joe/blog.html');
delete_dir($filepath)
參數: | $ filepath(字符串) - 要刪除的目錄的路徑 |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ filepath(字符串) - 要刪除的目錄的路徑
Returns: TRUE on success, FALSE on failure
Return type: bool
允許您刪除目錄及其包含的所有內容。用尾部斜杠提供目錄的源路徑。
重要
用這種方法非常小心!它將遞歸地刪除提供的路徑中的所有內容,包括子文件夾和所有文件。確保你的路徑是正確的。先嘗試使用list_files()
以驗證您的路徑是否正確。
$this->ftp->delete_dir('/public_html/path/to/folder/');
list_files([$path = '.'])
參數: | $ path(string) - 目錄路徑 |
---|---|
返回: | 文件的數組列表或失敗時的FALSE |
返回類型: | 排列 |
$ path(string) - 目錄路徑
返回:文件的數組列表或失敗時的FALSE
Return type: array
允許您檢索服務器上以文件形式返回的文件列表。您必須提供所需目錄的路徑。
$list = $this->ftp->list_files('/public_html/'); print_r($list);
mirror($locpath, $rempath)
參數: | $ locpath(字符串) - 本地路徑$ rempath(字符串) - 遠程路徑 |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ locpath(字符串) - 本地路徑
$ rempath(string) - 遠程路徑
Returns: TRUE on success, FALSE on failure
Return type: bool
遞歸讀取本地文件夾及其包含的所有內容(包括子文件夾),并基于此文件通過FTP創(chuàng)建鏡像。無論在服務器上重新創(chuàng)建原始文件路徑的目錄結構。您必須提供源路徑和目標路徑:
$this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
mkdir($path[, $permissions = NULL])
參數: | $ path(string) - 創(chuàng)建$ permissions的目錄路徑(int) - 權限(八進制) |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ path(string) - 要創(chuàng)建的目錄的路徑
$ permissions(int) - 權限(八進制)
Returns: TRUE on success, FALSE on failure
Return type: bool
讓您在服務器上創(chuàng)建一個目錄。提供以您希望創(chuàng)建的文件夾名稱結尾的路徑,并以斜杠結尾。
權限可以通過在第二個參數中傳遞八進制值來設置。
// Creates a folder named "bar" $this->ftp->mkdir('/public_html/foo/bar/', 0755);
chmod($path, $perm)
參數: | $ path(string) - 修改$ perm(int)權限的路徑 - 權限(八進制) |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ path(string) - 修改權限的路徑
$ perm(int) - 權限(八進制)
Returns: TRUE on success, FALSE on failure
Return type: bool
Permits you to set file permissions. Supply the path to the file or directory you wish to alter permissions on:
// Chmod "bar" to 755 $this->ftp->chmod('/public_html/foo/bar/', 0755);
changedir($path[, $suppress_debug = FALSE])
參數: | $ path(string) - 目錄路徑$ suppress_debug(bool) - 是否關閉此命令的調試消息 |
---|---|
返回: | 成功為TRUE,失敗為FALSE |
返回類型: | 布爾 |
$ path(string) - 目錄路徑
$ suppress_debug(bool) - 是否關閉此命令的調試消息
Returns: TRUE on success, FALSE on failure
Return type: bool
將當前工作目錄更改為指定的路徑。
$suppress_debug
如果您想將此方法用作is_dir()
FTP 的替代方法,該參數非常有用。
close()
返回: | 成功為TRUE,失敗為FALSE |
---|---|
返回類型: | 布爾 |