文件系統輔助函數?

目錄輔助函數文件包含的函數協助目錄運行。

加載文件系統輔助函數?

文件系統輔助函數使用下面的代碼加載:

helper('filesystem');

通用函數?

接下來的函數是通用的:

directory_map($source_dir[, $directory_depth = 0[, $hidden = FALSE]])?
參數:
  • $source_dir (string) – 資源目錄路徑
  • $directory_depth (int) – 遍歷目錄量度 (0 = 完全遞歸, 1 = 最近目錄, 等等)
  • $hidden (bool) – 是否包含隱藏目錄
返回:

文件數組

返回類型:

array

例如:

$map = directory_map('./mydirectory/');

注解

路徑幾乎常常與你的主要 index.php 文件有關系。

子文件夾包含的目錄還會被映射。如果你希望控制遞歸量度,你會使用秒參數(整型)。1 的量度將僅僅映射根層目錄:

$map = directory_map('./mydirectory/', 1);

默認情況下,在返回數組里將不會被包含隱藏文件。推翻這個運轉狀態,你也許要設置第三個參數為真(boolean):

$map = directory_map('./mydirectory/', FALSE, TRUE);

每一個文件名將是數組索引,它包含的文件將會被用數值編入索引。下面是一個典型數組:

Array (
        [libraries] => Array
                (
                        [0] => benchmark.html
                        [1] => config.html
                        ["database/"] => Array
                                (
                                        [0] => query_builder.html
                                        [1] => binds.html
                                        [2] => configuration.html
                                        [3] => connecting.html
                                        [4] => examples.html
                                        [5] => fields.html
                                        [6] => index.html
                                        [7] => queries.html
                                )
                        [2] => email.html
                        [3] => file_uploading.html
                        [4] => image_lib.html
                        [5] => input.html
                        [6] => language.html
                        [7] => loader.html
                        [8] => pagination.html
                        [9] => uri.html
                )

如果沒有找到結果,將會返回空數組。

write_file($path, $data[, $mode = 'wb'])?
參數:
  • $path (string) – File 路徑
  • $data (string) – 數據寫入 file
  • $mode (string) – fopen() 模式
返回:

如果寫入成功為 TRUE , 萬一錯誤是 FALSE

返回類型:

bool

將數據寫入指定路徑中的文件。如果文件不存在,這個函數將創建文件。

例如:

$data = 'Some file data';
if ( ! write_file('./path/to/file.php', $data))
{
        echo 'Unable to write the file';
}
else
{
        echo 'File written!';
}

你能隨意地通過第三個參數設置寫模式:

       write_file('./path/to/file.php', $data, 'r+');

默認模式是'wb'. 模式選項請查看 `PHP 用戶指導 <http://php.net/manual/en/function.fopen.php>`_ .

注解

這個函數向文件里寫入數據要按順序,它的權限必須被設置成可寫的。如果文件已經不存在, 那么目錄下的文件必須是可寫的。

注解

路徑關聯你的主站的 index.php 文件,不是你的 controller 或者 view 文件。 CodeIgniter 用前端 controller 因此路徑常常關聯主站的 index.

注解

當寫入文件時函數捕獲了文件上獨占的鎖定。

delete_files($path[, $del_dir = FALSE[, $htdocs = FALSE]])?
參數:
  • $path (string) – 目錄路徑
  • $del_dir (bool) – 是否也刪除目錄
  • $htdocs (bool) – 是否跳過刪除 .htaccess 和 index page 文件
返回:

萬一為FALSE,TRUE 為真

返回類型:

bool

刪除所有包含在備用路徑里的文件。

例如:

delete_files('./path/to/directory/');

如果第二個參數設置為 TRUE,包含備用根路徑的任何目錄將也會被刪除。

例如:

delete_files('./path/to/directory/', TRUE);

注解

文件必須是可寫的而已經歸屬至系統的文件原則上已被刪除。

get_filenames($source_dir[, $include_path = FALSE])?
參數:
  • $source_dir (string) – 目錄路徑
  • $include_path (bool) – 作為文件名的部分是否包含路徑
返回:

文件名數組

返回類型:

array

函數里取服務器路徑輸入并返回包含所有文件名的數組。設置第二參數為 TRUE 文件路徑能很隨意的被添加到文件名里。

例如:

$controllers = get_filenames(APPPATH.'controllers/');
get_dir_file_info($source_dir, $top_level_only)?
參數:
  • $source_dir (string) – 目錄路徑
  • $top_level_only (bool) – 是否僅僅查看特殊目錄 (不包含子目錄)
返回:

數組涵蓋的信息在備用目錄的內容中

返回類型:

array

閱讀指定的目錄并建立包含文件名,文件大小,日期和權限的數組。 如果傳送第二個參數被阻止成 FALSE 包含指定目錄的子文件夾一定是只讀的,如同這是個強調操作。

事例:

$models_info = get_dir_file_info(APPPATH.'models/');
get_file_info($file[, $returned_values = array('name', 'server_path', 'size', 'date')])?
參數:
  • $file (string) – File 路徑
  • $returned_values (array) – 任何返回的信息類型
返回:

在指定文件上的數組包含的信息或失效的 FALSE

返回類型:

array

約定的文件和路徑,文件返回(隨意地) the name, path, size and date modified 屬性信息。 第二參數允許你明確地聲明任何你想返回的信息。

有效的 $returned_values 選項是: name, size, date, readable, writeable, executablefileperms.

symbolic_permissions($perms)?
參數:
  • $perms (int) – 權限
返回:

象征權限的 string

返回類型:

string

抓取數值權限(就像是被 fileperms() 返回的)并且返回文件權限的標準符號記號。

echo symbolic_permissions(fileperms('./index.php'));  // -rw-r--r--
octal_permissions($perms)?
參數:
  • $perms (int) – 權限
返回:

八進制權限的 string

返回類型:

string

抓取數值權限(就像是被 fileperms() 返回的)并且返回文件權限的一個由三個字母組成的八進制記號。

echo octal_permissions(fileperms('./index.php')); // 644
set_realpath($path[, $check_existance = FALSE])?
參數:
  • $path (string) – 路徑
  • $check_existance (bool) – 如果路徑確實存在是否要去檢查
返回:

絕對路徑

返回類型:

string

函數會返回不帶符號鏈接的服務器路徑或者有關聯的目錄結構。 如果路徑不能決定選項的次一級爭議將觸發一個錯誤。

例如:

$file = '/etc/php5/apache2/php.ini';
echo set_realpath($file); //  輸出 '/etc/php5/apache2/php.ini'

$non_existent_file = '/path/to/non-exist-file.txt';
echo set_realpath($non_existent_file, TRUE);    // 顯示錯誤,如同路徑不能決定
echo set_realpath($non_existent_file, FALSE);   // 輸出 '/path/to/non-exist-file.txt'

$directory = '/etc/php5';
echo set_realpath($directory);  // 輸出 '/etc/php5/'

$non_existent_directory = '/path/to/nowhere';
echo set_realpath($non_existent_directory, TRUE);       // 顯示錯誤,如同路徑不能決定
echo set_realpath($non_existent_directory, FALSE);      // 輸出 '/path/to/nowhere'