thomas-squall / file-utils
文件管理工具
0.2.8
2022-07-10 14:59 UTC
Requires
- ext-json: *
- thomas-squall/string-utils: ^0.5.5
README
可用函数列表
- require_all_files
- require_once_all_files
- include_all_files
- include_once_all_files
- get_all_files
- get_all_dirs
- file_get_json
- file_put_json
require_all_files
描述
需要匹配提供的模式的文件夹中的所有文件。
定义
require_all_files($dir, $pattern = "*.php", $callback = null)
其中
- $dir 是文件所在的目录
- $pattern 是用于查找文件的模式 (*.php 为默认值)
- $recursive 如果设置为 true 将获取子文件夹(默认为 false)
- $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)
其中
- $dir 是文件所在的目录
- $pattern 是用于查找文件的模式 (*.php 为默认值)
- $recursive 如果设置为 true 将获取子文件夹(默认为 false)
- $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)
其中
- $dir 是文件所在的目录
- $pattern 是用于查找文件的模式 (*.php 为默认值)
- $recursive 如果设置为 true 将获取子文件夹(默认为 false)
- $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)
其中
- $dir 是文件所在的目录
- $pattern 是用于查找文件的模式 (*.php 为默认值)
- $recursive 如果设置为 true 将获取子文件夹(默认为 false)
- $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)
其中
- $dir 是文件所在的目录
- $recursive 如果设置为 true 将获取子文件夹(默认为 false)
用法
print_r(get_all_files("my/dir"));
get_all_dirs
描述
返回给定目录中的所有目录。
定义
get_all_dirs($dir, $recursive = false)
其中
- $dir 是文件所在的目录
- $recursive 如果设置为 true 将获取子目录(默认为 false)
用法
print_r(get_all_files("my/dir"));
file_get_json
描述
读取 json 文件的内容并将其作为数组或对象返回。请注意:如果内容不是 json 编码字符串,则返回空数组。
定义
file_get_json($file, $associative = true)
其中
- $file 是要读取的文件的路径
- $associative 当为 true 时,将内容作为关联数组返回,当为 false 时,作为对象返回
用法
$my_array = file_get_json("my/dir/my_file.json");
file_put_json
描述
将数组或对象保存到 json 文件中。
定义
file_put_json($file, $content, $pretty = true)
其中
- $file 是要保存内容的文件的路径
- $content 是要保存的内容(数组或对象)
- $pretty 当为 true 时,将内容保存为美化格式,当为 false 时,保存为最小化格式
用法
$my_array = [ 'fruit_1' => 'apple', 'fruit_2' => 'pear', 'fruit_3' => 'orange' ]; file_put_json("my/dir/my_file.json", $my_array);