CodeIgniter 使用手冊版本 2.1.4


FTP 類別

CodeIgniter的FTP 類別允許你將檔案傳輸至遠端伺服器。並且也可對遠端檔案作移動、重命名及刪除的動作。除此之外,FTP類別亦包含一"mirroring"函數,允許對本地端的整個目錄,經由FTP協定,在遠端主機上作鏡像的動作。

注意:  FTP類別目前僅支援標準FTP,不包含SFTP及SSL FTP協定。

初始化類別

如同CodeIgniter的其他類別,FTP類別可在控制器(controller)裡,透過 $this->load->library 函式來初始化,如下所示:

$this->load->library('ftp');

一旦載入此類別,便可以: $this->ftp 的方式來存取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();

函數說明

$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參數

如果你想將FTP相關參數儲存在設定檔裡,僅需建立一名為ftp.php的檔案,將$config陣列 增加到該檔案中,並儲存至config/ftp.php,如此一來,該設定檔便會自動被讀取。

可用連線參數:

$this->ftp->upload()

上傳一檔案至伺服器,本地端路徑及遠端路徑為必要參數,傳輸模式及檔案權限為選擇性參數。參考範例如下:

$this->ftp->upload('/local/path/to/myfile.html','/public_html/myfile.html','ascii',0775);

傳輸模式包含:  asciibinary,及auto (預設值)。若是選擇 auto 模式,則會根據來源檔的副檔名來決定所要使用的傳輸模式。

Permissions can be passed as an octal value in the fourth parameter.

$this->ftp->download()

Downloads a file from your server. You must supply the remote path and the local path, and you can optionally set the mode. Example:

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

Returns FALSE if the download does not execute successfully (including if PHP does not have permission to write the local file)

$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建立此目錄之鏡像。來源目錄之完整結構皆會被複製到伺服器端,需提供來源路徑及目的路徑作為參數:

$this->ftp->mirror('/path/to/myfolder/','/public_html/myfolder/');

$this->ftp->mkdir()

在伺服器上建立一目錄。需給定欲建立目錄之完整路徑(包含結尾斜線)以作為參數。並可透過傳遞一八進制權限值至第二個參數以設定檔案權限。

// 建立一名為"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();

關閉對伺服器的連線,建議在完成上傳動作後使用此函數以關閉連線。