thomas-squall/file-utils

文件管理工具

0.2.8 2022-07-10 14:59 UTC

This package is auto-updated.

Last update: 2024-09-10 19:53:51 UTC


README

可用函数列表

  1. require_all_files
  2. require_once_all_files
  3. include_all_files
  4. include_once_all_files
  5. get_all_files
  6. get_all_dirs
  7. file_get_json
  8. file_put_json

require_all_files

描述

需要匹配提供的模式的文件夹中的所有文件。

定义

require_all_files($dir, $pattern = "*.php", $callback = null)

其中

  1. $dir 是文件所在的目录
  2. $pattern 是用于查找文件的模式 (*.php 为默认值)
  3. $recursive 如果设置为 true 将获取子文件夹(默认为 false)
  4. $callback 是对每个文件调用的回调(默认为 null,文件路径将被作为参数传递)

用法

function callback($file) {
    echo "file $file has been loaded..." . EOF;
}

require_all_fields("my/dir", "*.php", "callback");

require_once_all_files

描述

需要匹配提供的模式的文件夹中的所有文件。

定义

require_once_all_files($dir, $pattern = "*.php", $callback = null)

其中

  1. $dir 是文件所在的目录
  2. $pattern 是用于查找文件的模式 (*.php 为默认值)
  3. $recursive 如果设置为 true 将获取子文件夹(默认为 false)
  4. $callback 是对每个文件调用的回调(默认为 null,文件路径将被作为参数传递)

用法

function callback($file) {
    echo "file $file has been loaded..." . EOF;
}

require_once_all_fields("my/dir", "*.php", "callback");

include_all_files

描述

包含匹配提供的模式的文件夹中的所有文件。

定义

include_all_files($dir, $pattern = "*.php", $callback = null)

其中

  1. $dir 是文件所在的目录
  2. $pattern 是用于查找文件的模式 (*.php 为默认值)
  3. $recursive 如果设置为 true 将获取子文件夹(默认为 false)
  4. $callback 是对每个文件调用的回调(默认为 null,文件路径将被作为参数传递)

用法

function callback($file) {
    echo "file $file has been loaded..." . EOF;
}

include_all_fields("my/dir", "*.php", "callback");

include_once_all_files

描述

包含匹配提供的模式的文件夹中的所有文件。

定义

include_once_all_files($dir, $pattern = "*.php", $callback = null)

其中

  1. $dir 是文件所在的目录
  2. $pattern 是用于查找文件的模式 (*.php 为默认值)
  3. $recursive 如果设置为 true 将获取子文件夹(默认为 false)
  4. $callback 是对每个文件调用的回调(默认为 null,文件路径将被作为参数传递)

用法

function callback($file) {
    echo "file $file has been loaded..." . EOF;
}

include_once_all_files("my/dir", "*.php", "callback");

get_all_files

描述

获取匹配提供的模式的文件夹中的所有文件。

定义

get_all_files($dir, $recursive = false)

其中

  1. $dir 是文件所在的目录
  2. $recursive 如果设置为 true 将获取子文件夹(默认为 false)

用法

print_r(get_all_files("my/dir"));

get_all_dirs

描述

返回给定目录中的所有目录。

定义

get_all_dirs($dir, $recursive = false)

其中

  1. $dir 是文件所在的目录
  2. $recursive 如果设置为 true 将获取子目录(默认为 false)

用法

print_r(get_all_files("my/dir"));

file_get_json

描述

读取 json 文件的内容并将其作为数组或对象返回。请注意:如果内容不是 json 编码字符串,则返回空数组。

定义

file_get_json($file, $associative = true)

其中

  1. $file 是要读取的文件的路径
  2. $associative 当为 true 时,将内容作为关联数组返回,当为 false 时,作为对象返回

用法

$my_array = file_get_json("my/dir/my_file.json");

file_put_json

描述

将数组或对象保存到 json 文件中。

定义

file_put_json($file, $content, $pretty = true)

其中

  1. $file 是要保存内容的文件的路径
  2. $content 是要保存的内容(数组或对象)
  3. $pretty 当为 true 时,将内容保存为美化格式,当为 false 时,保存为最小化格式

用法

$my_array = [
    'fruit_1' => 'apple',
    'fruit_2' => 'pear',
    'fruit_3' => 'orange' 
];

file_put_json("my/dir/my_file.json", $my_array);

更多工具即将推出...