Skip to content
This repository was archived by the owner on Jul 30, 2026. It is now read-only.

Files and Paths

Josh edited this page Aug 6, 2017 · 7 revisions

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)

data_uri()

Return a file as a Data-URI for, e.g., embedding in HTML.

Arguments

Type Description
string File path.

Returns

Returns a Data-URI string on success or FALSE if the file could not be opened.

Example

<img src="<?=\blobfolio\common\file::data_uri('/path/to/world.jpg')?>" />

empty_dir()

Determine whether a directory is empty.

Arguments

Type Description
string File path.

Returns

Returns TRUE if the directory is empty, FALSE if it isn't or if the path was unreadable.

leadingslash()

Ensure the path has a leading "/".

Versions

  • By Value
  • By Reference

Arguments

Type Description Notes
string, array File path. If an array is passed, each value will be processed recursively.

Returns

Returns the path with a leading "/" if passed by value, otherwise TRUE.

Example

// By value.
$foo = \blobfolio\common\file::leadingslash('hello'); // /hello

// By reference.
\blobfolio\common\ref\file::leadingslash($foo);

path()

Convert backslashes to forwardslashes, end directories with a trailing slash, expand symlinks, and resolve to absolute paths (when possible).

Versions

  • By Value
  • By Reference

Arguments

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

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.

Example

// 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);

readfile_chunked()

This buffers the contents of a file in chunks, greatly reducing the strain on a web server when transmitting large files through PHP.

Arguments

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

Returns

The contents of the file are buffered and flushed. The function returns either the byte count or status of the read.

redirect()

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.

Arguments

Type Description
string URL.

Returns

N/A

rmdir()

Recursively remove a directory.

Arguments

Type Description
string Directory path.

Returns

Returns TRUE if the directory was removed, FALSE if there was any sort of problem.

trailingslash()

Ensure a path ends in a "/".

Versions

  • By Value
  • By Reference

Arguments

Type Description Notes
string, array File path. If an array is passed, each value will be processed recursively.

Returns

Returns the slashed path if passed by value, otherwise TRUE.

unixslash()

Convert "\" to "/" and fix a few stupid things along the way.

Versions

  • By Value
  • By Reference

Arguments

Type Description Notes
string, array File path. If an array is passed, each value will be processed recursively.

Returns

Returns the slashed path if passed by value, otherwise TRUE.

unleadingslash()

Remove the leading slash on a path, if any.

Versions

  • By Value
  • By Reference

Arguments

Type Description Notes
string, array File path. If an array is passed, each value will be processed recursively.

Returns

Returns the unslashed path if passed by value, otherwise TRUE.

unparse_url()

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.

Arguments

Type Description Notes
array URL parts. The structure should match output from parse_url().

Returns

Returns a URL as a string or FALSE if invalid.

Example

$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=Foobar

untrailingslash()

Remove the tailing slash from a path, if any.

Versions

  • By Value
  • By Reference

Arguments

Type Description Notes
string, array File path. If an array is passed, each value will be processed recursively.

Returns

Returns the unslashed path if passed by value, otherwise TRUE.

CLI

Constants

Dom Helpers

Files and Paths

Formatting

General Data Helpers

Images

Multi-Byte Wrappers

Sanitizing and Validation

Typecasting

Clone this wiki locally