-
Notifications
You must be signed in to change notification settings - Fork 0
Files and Paths
Why are filesystems such a bitch? Haha. This class contains functions to help with filesystem tasks and path manipulation.
Use:
\blobfolio\common\file (by value)
\blobfolio\common\ref\file (by reference)
Recursively copy files and/or directories, attempting to preserve file permissions too.
| Type | Description |
|---|---|
string |
Source path. |
string |
Destination path. |
Returns TRUE if the last copy operation succeeded, FALSE otherwise.
This method can be used to return a simple column map for CSV headers. By default it will return an array containing all headers and their positions, but if provided an array of specific headers, only those will be returned.
| Type | Description | Default |
|---|---|---|
string |
CSV file path. | |
array |
Header(s) to find. See below for more information. | All columns. |
string |
Delimiter. | "," |
If successful, an associative array is returned using the header values as keys, column positions as values. FALSE is returned on failure.
If an array containing specific headers is passed, only those values are returned. These headers may be specified as either a simple array (e.g. ["one", "two"]) or an associative array keyed with nicer labels (e.g. ["email"=>"some_stupid_long_header"]). In cases of the latter, the nice key ("email") will be used for the return value rather than the true cell value.
Return a file as a Data-URI for, e.g., embedding in HTML.
| Type | Description |
|---|---|
string |
File path. |
Returns a Data-URI string on success or FALSE if the file could not be opened.
<img src="<?=\blobfolio\common\file::data_uri('/path/to/world.jpg')?>" />Recursively count up all the bytes of all the files under a given directory.
Be careful pointing this at very large directories; the number of operations can quickly add up. :)
| Type | Description |
|---|---|
string |
Directory path. |
This function always returns an integer representing the cumulative byte size. If a directory is invalid or empty, 0 is returned.
Determine whether a directory is empty.
| Type | Description |
|---|---|
string |
File path. |
Returns TRUE if the directory is empty, FALSE if it isn't or if the path was unreadable.
This function will recursively hash all files within a directory, and return a hash of that result. This can be used to e.g. detect deep content changes.
Note: The file hashing component can be resource-intensive. Be careful combining heavy algorithms with large directories. See here for supported algorithms.
| Type | Description | Default |
|---|---|---|
string |
Directory path. | |
string |
Algorithm to use for the final result. | MD5 |
string |
Algorithm to use for individual files. | Same as result. |
If either the directory or result hashing algorithm are invalid, FALSE is returned. If a directory is empty, a hash of the string "empty" is returned. Otherwise a hash of all file hashes is returned.
Ensure the path has a leading "/".
- By Value
- By Reference
| Type | Description | Notes |
|---|---|---|
string, array
|
File path. | If an array is passed, each value will be processed recursively. |
Returns the path with a leading "/" if passed by value, otherwise TRUE.
// By value.
$foo = \blobfolio\common\file::leadingslash('hello'); // /hello
// By reference.
\blobfolio\common\ref\file::leadingslash($foo);Count the number of lines in a text file as efficiently as possible.
| Type | Description | Default |
|---|---|---|
string |
File path. | |
bool |
Ignore empty lines or lines consisting only of whitespace. | TRUE |
This method returns an integer representing the number of lines. If a file is invalid or empty, 0 is returned.
Recursively make a directory. While the native PHP mkdir() function has a recursive option, for some reason it does not consistently apply the desired permissions to all new nested directories.
| Type | Description | Default |
|---|---|---|
string |
Directory path. | |
mixed |
Octal permissions like 0755. |
If not specified, the class' base directory permissions will be used. |
Returns TRUE if the directories were created (or already existed), otherwise FALSE.
Convert backslashes to forwardslashes, end directories with a trailing slash, expand symlinks, and resolve to absolute paths (when possible).
- By Value
- By Reference
| Type | Description | Notes | Default |
|---|---|---|---|
string, array
|
File path. | If an array is passed, each value will be processed recursively. | |
bool |
Validate path. | If TRUE, the path must exist and be readable. |
TRUE |
Returns the sanitized path if passed by value. If validating and the path is bad, FALSE is returned. When passed by value, TRUE or FALSE is returned.
// By value.
$foo = \blobfolio\common\file::path('..\hello'); // /var/www/foobar/hello/
$foo = \blobfolio\common\file::path('../hello', true); // FALSE
// By reference.
\blobfolio\common\ref\file::path($foo);This buffers the contents of a file in chunks, greatly reducing the strain on a web server when transmitting large files through PHP.
| Type | Description | Notes | Default |
|---|---|---|---|
string |
File path. | ||
bool |
Return bytes. | When TRUE, the function returns the number of bytes like readfile(), otherwise the status is returned. |
TRUE |
The contents of the file are buffered and flushed. The function returns either the byte count or status of the read.
Unset $_REQUEST data (if any) and issue a redirect to another location. By default this will be accomplished by sending a "Location" header, but if headers have already been sent it will output Javascript instead.
| Type | Description |
|---|---|
string |
URL. |
N/A
Recursively remove a directory.
| Type | Description |
|---|---|
string |
Directory path. |
Returns TRUE if the directory was removed, FALSE if there was any sort of problem.
Recursively build a list of directories and/or files under a given path. Unlike PHP's native scandir(), . and .. paths are never included.
| Type | Description | Default |
|---|---|---|
string |
Directory path. | |
bool |
Include files. | TRUE |
bool |
Include directories. | TRUE |
int |
Max depth. |
-1 (infinite) |
This method always returns an array.
Ensure a path ends in a "/".
- By Value
- By Reference
| Type | Description | Notes |
|---|---|---|
string, array
|
File path. | If an array is passed, each value will be processed recursively. |
Returns the slashed path if passed by value, otherwise TRUE.
Convert "\" to "/" and fix a few stupid things along the way.
- By Value
- By Reference
| Type | Description | Notes |
|---|---|---|
string, array
|
File path. | If an array is passed, each value will be processed recursively. |
Returns the slashed path if passed by value, otherwise TRUE.
Remove the leading slash on a path, if any.
- By Value
- By Reference
| Type | Description | Notes |
|---|---|---|
string, array
|
File path. | If an array is passed, each value will be processed recursively. |
Returns the unslashed path if passed by value, otherwise TRUE.
The opposite of parse_url(), this will rebuild a URL given an array of parts.
Note: like parse_url(), this doesn't imply the result is actually a valid URL.
| Type | Description | Notes |
|---|---|---|
array |
URL parts. | The structure should match output from parse_url(). |
Returns a URL as a string or FALSE if invalid.
$parsed = parse_url('https://google.com?s=Foobar');
/*
Array
(
[scheme] => https
[host] => google.com
[query] => s=Foobar
)
*/
echo \blobfolio\common\file::unparse_url($parsed); // https://google.com?s=FoobarRemove the tailing slash from a path, if any.
- By Value
- By Reference
| Type | Description | Notes |
|---|---|---|
string, array
|
File path. | If an array is passed, each value will be processed recursively. |
Returns the unslashed path if passed by value, otherwise TRUE.
CLI
Constants
Dom Helpers
- ::get_nodes_by_class()
- ::innerhtml()
- ::load_svg()
- ::parse_css()
- ::remove_namespace()
- ::remove_node()
- ::remove_nodes()
- ::save_svg()
Files and Paths
- ::copy()
- ::csv_headers()
- ::data_uri()
- ::dirsize()
- ::empty_dir()
- ::hash_dir()
- ::leadingslash()
- ::line_count()
- ::mkdir()
- ::path()
- ::readfile_chunked()
- ::redirect()
- ::rmdir()
- ::scandir()
- ::trailingslash()
- ::unixslash()
- ::unleadingslash()
- ::unparse_url()
- ::untrailingslash()
Formatting
- ::array_flatten()
- ::array_to_indexed()
- ::ceil()
- ::cidr_to_range()
- ::decode_entities()
- ::decode_escape_entities()
- ::decode_js_entities()
- ::decode_unicode_entities()
- ::excerpt()
- ::floor()
- ::fraction()
- ::inflect()
- ::ip_to_number()
- ::ip_to_subnet()
- ::json()
- ::json_decode()
- ::json_encode()
- ::links()
- ::list_to_array()
- ::money()
- ::number_to_ip()
- ::phone()
- ::round()
- ::to_csv()
- ::to_timezone()
- ::to_xls()
General Data Helpers
- ::array_compare()
- ::array_idiff()
- ::array_iintersect()
- ::array_ikey_exists()
- ::array_isearch()
- ::array_map_recursive()
- ::array_otherize()
- ::array_pop()
- ::array_pop_rand()
- ::array_pop_top()
- ::cc_exp_months()
- ::cc_exp_years()
- ::datediff()
- ::iin_array()
- ::in_range()
- ::ip_in_range()
- ::is_json()
- ::is_utf8()
- ::json_decode_array()
- ::length_in_range()
- ::parse_args()
- ::random_int()
- ::random_string()
- ::switcheroo()
- ::unsetcookie()
Images
Multi-Byte Wrappers
- ::parse_str()
- ::parse_url()
- ::str_pad()
- ::str_split()
- ::strlen()
- ::strpos()
- ::strrev()
- ::strrpos()
- ::strtolower()
- ::strtoupper()
- ::substr()
- ::substr_count()
- ::trim()
- ::ucfirst()
- ::ucwords()
- ::wordwrap()
Sanitizing and Validation
- ::accents()
- ::attribute_value()
- ::au_state()
- ::ca_postal_code()
- ::cc()
- ::control_characters()
- ::country()
- ::csv()
- ::date()
- ::datetime()
- ::domain()
- ::ean()
- ::email()
- ::file_extension()
- ::html()
- ::hostname()
- ::ip()
- ::iri_value()
- ::isbn()
- ::js()
- ::mime()
- ::name()
- ::password()
- ::printable()
- ::province()
- ::quotes()
- ::state()
- ::svg()
- ::timezone()
- ::to_range()
- ::upc()
- ::url()
- ::utf8()
- ::whitespace()
- ::whitespace_multiline()
- ::zip5()
Typecasting