牛骨文教育服务平台(让学习变的简单)

文件辅助函数

文件辅助函数文件包含了一些帮助你处理文件的函数。

加载辅助函数

该辅助函数通过下面的代码加载:

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

可用函数

该辅助函数有下列可用函数:

read_file($file)

参数:

  • $file (string) -- File path

返回: File contents or FALSE on failure

返回类型: string

返回指定文件的内容。

例如:

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

可以是相对路径或绝对路径,如果失败返回 FALSE 。

注解

路径是相对于你网站的 index.php 文件的,而不是相对于控制器或视图文件。 这是因为 CodeIgniter 使用的前端控制器,所以所有的路径都是相对于 index.php 所在路径。

注解

该函数已废弃,使用 PHP 的原生函数 file_get_contents() 代替。

重要

如果你的服务器配置了 open_basedir 限制,该函数可能无法访问限制之外的文件。

write_file($path, $data[, $mode = "wb"])

参数:

  • $path (string) -- File path
  • $data (string) -- Data to write to file
  • $mode (string) -- fopen() mode

返回: TRUE if the write was successful, FALSE in case of an error

返回类型: 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 用户指南 了解写模式的选项。

注解

路径是相对于你网站的 index.php 文件的,而不是相对于控制器或视图文件。 这是因为 CodeIgniter 使用的前端控制器,所以所有的路径都是相对于 index.php 所在路径。

注解

该函数在写入文件时会申请一个排他性锁。

delete_files($path[, $del_dir = FALSE[, $htdocs = FALSE]])

参数:

  • $path (string) -- Directory path
  • $del_dir (bool) -- Whether to also delete directories
  • $htdocs (bool) -- Whether to skip deleting .htaccess and index page files

返回: TRUE on success, FALSE in case of an error

返回类型: bool

删除指定路径下的所有文件。

例如:

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

如果第二个参数设置为 TRUE ,那么指定路径下的文件夹也一并删除。

例如:

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

注解

要被删除的文件必须是当前系统用户所有或者是当前用户对之具有写权限。

get_filenames($source_dir[, $include_path = FALSE])

参数:

  • $source_dir (string) -- Directory path
  • $include_path (bool) -- Whether to include the path as part of the filenames

返回: An array of file names

返回类型: array

获取指定目录下所有文件名组成的数组。如果需要完整路径的文件名, 可以将第二个参数设置为 TRUE 。

例如:

$controllers = get_filenames(APPPATH."controllers/");

get_dir_file_info($source_dir, $top_level_only)

参数:

  • $source_dir (string) -- Directory path
  • $top_level_only (bool) -- Whether to look only at the specified directory (excluding sub-directories)

返回: An array containing info on the supplied directory"s contents

返回类型: 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 path
  • $returned_values (array) -- What type of info to return

返回: An array containing info on the specified file or FALSE on failure

返回类型: array

获取指定文件的信息,包括文件名、路径、文件大小,修改日期等。第二个参数可以用于 声明只返回回你想要的信息。

第二个参数 $returned_values 有效的值有:name、size、date、readable、writeable、 executable 和 fileperms 。

get_mime_by_extension($filename)

参数:

  • $filename (string) -- File name

返回: MIME type string or FALSE on failure

返回类型: string

根据 config/mimes.php 文件中的配置将文件扩展名转换为 MIME 类型。 如果无法判断 MIME 类型或 MIME 配置文件读取失败,则返回 FALSE 。

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

注解

这个函数只是一种简便的判断 MIME 类型的方法,并不准确,所以 请不要用于安全相关的地方。

symbolic_permissions($perms)

参数:

  • $perms (int) -- Permissions

返回: Symbolic permissions string

返回类型: string

将文件权限的数字格式(譬如 fileperms() 函数的返回值)转换为标准的符号格式。

echo symbolic_permissions(fileperms("./index.php"));  // -rw-r--r--

octal_permissions($perms)

参数:

  • $perms (int) -- Permissions

返回: Octal permissions string

返回类型: string

将文件权限的数字格式(譬如 fileperms() 函数的返回值)转换为三个字符的八进制表示格式。

echo octal_permissions(fileperms("./index.php")); // 644