-
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.
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')?>" />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);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 |
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