CodeIgniter 使用手冊版本 2.2.0


File 輔助函數

File 函數用來處理檔案讀寫相關資訊。

載入輔助函數

載入 File 輔助函數

$this->load->helper('file');

下列為可以使用的函數:

read_file('path')

回傳指定路徑之檔案相關內容。參考範例:

$string = read_file('./path/to/file.php');

檔案路徑可以是絕對路徑或者是相關路徑。如果開檔失敗則回傳錯誤(boolean False)。

注意: 檔案路徑是相對於網站根目錄 index.php 檔案,而不是相對路徑於您的控制器(controller)或是顯示(view)的檔案。 CodeIgniter 所使用的前端控制器都是相對於根目錄的 index 檔案。

假如您的伺服器有針對 open_basedir 做限制,這樣您就不能利用上面函式存取檔案。

write_file('path', $data)

將資料寫入到指定的檔案裡面,假如此檔案不存在,函式將會自動建立新檔案。參考範例:

$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 user guide

注意: 要使用此函數寫資料到檔案,請先確定該檔案是否可以"寫入",如果該檔案不存在則該使用者應該對包含該檔案的目錄有寫入權限(即 UNIX 底下的 666, 777 權限)。

注意: 檔案路徑是相對於網站根目錄 index.php 檔案,而不是相對路徑於您的控制器(controller)或是顯示(view)的檔案。 CodeIgniter 所使用的前端控制器都是相對於根目錄的 index 檔案。

delete_files('path')

刪除指定目錄底下所有檔案。參考範例:

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

假設第二個參數設定為 true,指定目錄底下的所有檔案包含子目錄都會被刪除。參考範例:

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

注意: 要被刪除的檔案或者是目錄都必須是該使用者可以寫入的權限,這樣才可以刪除。

get_filenames('path/to/directory/')

Takes a server path as input and returns an array containing the names of all files contained within it. The file path can optionally be added to the file names by setting the second parameter to TRUE.

get_dir_file_info('path/to/directory/', $top_level_only = TRUE)

Reads the specified directory and builds an array containing the filenames, filesize, dates, and permissions. Sub-folders contained within the specified path are only read if forced by sending the second parameter, $top_level_only to FALSE, as this can be an intensive operation.

get_file_info('path/to/file',$file_information)

Given a file and path,returns the name,path,size,date modified. Second parameter allows you to explicitly declare what information you want returned; options are: name,server_path,size,date,readable,writable,executable,fileperms. Returns FALSE if the file cannot be found.

注意: The "writable" uses the PHP function is_writable() which is known to have issues on the IIS webserver. Consider using fileperms instead,which returns information from PHP's fileperms() function.

get_mime_by_extension('file')

Translates a file extension into a mime type based on config/mimes.php. Returns FALSE if it can't determine the type,or open the mime config file.

$file = "somefile.png";
echo $file . ' is has a mime type of ' . get_mime_by_extension($file);

注意: This is not an accurate way of determining file mime types,and is here strictly as a convenience. It should not be used for security.

symbolic_permissions($perms)

Takes numeric permissions (such as is returned by fileperms() and returns standard symbolic notation of file permissions.

echo symbolic_permissions(fileperms('./index.php'));

// -rw-r--r--

octal_permissions($perms)

Takes numeric permissions (such as is returned by fileperms() and returns a three character octal notation of file permissions.

echo octal_permissions(fileperms('./index.php'));

// 644