thomas-squall / file-utils
Utilities for file management
Requires
- ext-json: *
- thomas-squall/string-utils: ^0.5.5
README
List of available functions
- 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
Description
Requires all the files in a folder that matches the provided pattern.
Definition
require_all_files($dir, $pattern = "*.php", $callback = null)
Where:
- $dir is the directory where the files are located
- $pattern is the pattern used to find the files (*.php by default)
- $recursive will fetch also the child folders if set to true (default is false)
- $callback is the callback called per each file (null by default, the file path will be passed as parameter)
Usage
function callback($file) { echo "file $file has been loaded..." . EOF; } require_all_fields("my/dir", "*.php", "callback");
require_once_all_files
Description
Requires once all the files in a folder that matches the provided pattern.
Definition
require_once_all_files($dir, $pattern = "*.php", $callback = null)
Where:
- $dir is the directory where the files are located
- $pattern is the pattern used to find the files (*.php by default)
- $recursive will fetch also the child folders if set to true (default is false)
- $callback is the callback called per each file (null by default, the file path will be passed as parameter)
Usage
function callback($file) { echo "file $file has been loaded..." . EOF; } require_once_all_fields("my/dir", "*.php", "callback");
include_all_files
Description
Includes all the files in a folder that matches the provided pattern.
Definition
include_all_files($dir, $pattern = "*.php", $callback = null)
Where:
- $dir is the directory where the files are located
- $pattern is the pattern used to find the files (*.php by default)
- $recursive will fetch also the child folders if set to true (default is false)
- $callback is the callback called per each file (null by default, the file path will be passed as parameter)
Usage
function callback($file) { echo "file $file has been loaded..." . EOF; } include_all_fields("my/dir", "*.php", "callback");
include_once_all_files
Description
Includes once all the files in a folder that matches the provided pattern.
Definition
include_once_all_files($dir, $pattern = "*.php", $callback = null)
Where:
- $dir is the directory where the files are located
- $pattern is the pattern used to find the files (*.php by default)
- $recursive will fetch also the child folders if set to true (default is false)
- $callback is the callback called per each file (null by default, the file path will be passed as parameter)
Usage
function callback($file) { echo "file $file has been loaded..." . EOF; } include_once_all_files("my/dir", "*.php", "callback");
get_all_files
Description
Get all the files in a folder that matches the provided pattern.
Definition
get_all_files($dir, $recursive = false)
Where:
- $dir is the directory where the files are located
- $recursive will fetch also the children folders if set to true (default is false)
Usage
print_r(get_all_files("my/dir"));
get_all_dirs
Description
Returns all the directories in a given directory.
Definition
get_all_dirs($dir, $recursive = false)
Where:
- $dir is the directory where the files are located
- $recursive will fetch also the children directories if set to true (default is false)
Usage
print_r(get_all_files("my/dir"));
file_get_json
Description
Reads the content of a json file and returns it as array or object. Please Note: if the content is not a json encoded string an empty array will be returned.
Definition
file_get_json($file, $associative = true)
Where:
- $file is the path to the file that we want to read
- $associative will return the content as associative array when true, as object when false
Usage
$my_array = file_get_json("my/dir/my_file.json");
file_put_json
Description
Saves an array or an object into a json file.
Definition
file_put_json($file, $content, $pretty = true)
Where:
- $file is the path to the file that we want to save the content to
- $content is the content we want to save (array or object)
- $pretty will save the content prettified when true, minified when false
Usage
$my_array = [ 'fruit_1' => 'apple', 'fruit_2' => 'pear', 'fruit_3' => 'orange' ]; file_put_json("my/dir/my_file.json", $my_array);