class File (View source)

File helper.

Functions useful for working with files and directories.

Used as follows:

$file = Core::make('helper/file');
$path = 'http://www.concrete5.org/tools/get_latest_version_number';
$contents = $file->getContents($path);
echo $contents;

\@package Helpers

Methods

getDirectoryContents($dir, $ignoreFiles = [], $recursive = false)

Returns the contents of a directory.

string
unfilename(string $filename)

Removes the extension of a filename, uncamelcases it.

copyAll(string $source, string $target, int $mode = null)

Recursively copies all items in the source directory or file to the target directory.

getCreateFilePermissions(string $path = null)

Returns an object with two permissions modes (octal): one for files: $res->file and another for directories: $res->dir.

bool
removeAll(string $source, bool $inc = false)

Removes all files from within a specified directory.

forceDownload(stings $file)

Takes a path to a file and sends it to the browser, streaming it, and closing the HTTP connection afterwards. Basically a force download method.

string
getTemporaryDirectory()

Returns the full path to the temporary directory.

bool
append(string $filename, string $content)

Adds content to a new line in a file. If a file is not there it will be created.

string|bool
getContents($file, string $timeout = null)

Just a consistency wrapper for file_get_contents Should use curl if it exists and fopen isn't allowed (thanks Remo).

bool
clear($file)

Removes contents of the file.

string
sanitize(string $file)

Cleans up a filename and returns the cleaned up version.

array
splitFilename(string $filename)

Splits a filename into directory, base file name, extension.

string
getExtension(string $filename)

Returns the extension for a file name.

string
replaceExtension(string $filename, string $extension)

Takes a path and replaces the files extension in that path with the specified extension.

bool
isSamePath(string $path1, string $path2)

Checks if two path are the same, considering directory separator and OS case sensitivity.

makeExecutable(string $file, string $who = 'all')

Try to set the executable bit of a file.

Details

getDirectoryContents($dir, $ignoreFiles = [], $recursive = false)

Returns the contents of a directory.

Parameters

$dir
$ignoreFiles
$recursive

string unfilename(string $filename)

Removes the extension of a filename, uncamelcases it.

Parameters

string $filename

Return Value

string

copyAll(string $source, string $target, int $mode = null)

Recursively copies all items in the source directory or file to the target directory.

Parameters

string $source

Source dir/file to copy

string $target

Place to copy the source

int $mode

What to chmod the file to

stdClass getCreateFilePermissions(string $path = null)

Returns an object with two permissions modes (octal): one for files: $res->file and another for directories: $res->dir.

Parameters

string $path

(optional)

Return Value

stdClass

bool removeAll(string $source, bool $inc = false)

Removes all files from within a specified directory.

Parameters

string $source

Directory

bool $inc

Remove the passed directory as well or leave it alone

Return Value

bool

Whether the methods succeeds or fails

forceDownload(stings $file)

Takes a path to a file and sends it to the browser, streaming it, and closing the HTTP connection afterwards. Basically a force download method.

Parameters

stings $file

string getTemporaryDirectory()

Returns the full path to the temporary directory.

Return Value

string

bool append(string $filename, string $content)

Adds content to a new line in a file. If a file is not there it will be created.

Parameters

string $filename
string $content

Return Value

bool

string|bool getContents($file, string $timeout = null)

Just a consistency wrapper for file_get_contents Should use curl if it exists and fopen isn't allowed (thanks Remo).

Parameters

$file
string $timeout

Return Value

string|bool

Returns false in case of failure

Exceptions

TimeoutException

Request timed out

bool clear($file)

Removes contents of the file.

Parameters

$file

Return Value

bool

string sanitize(string $file)

Cleans up a filename and returns the cleaned up version.

Parameters

string $file

Return Value

string

array splitFilename(string $filename)

Splits a filename into directory, base file name, extension.

If the file name starts with a dot and it's the only dot (eg: '.htaccess'), we don't consider the file to have an extension.

Parameters

string $filename

Return Value

array

string getExtension(string $filename)

Returns the extension for a file name.

Parameters

string $filename

Return Value

string

string replaceExtension(string $filename, string $extension)

Takes a path and replaces the files extension in that path with the specified extension.

Parameters

string $filename
string $extension

Return Value

string

bool isSamePath(string $path1, string $path2)

Checks if two path are the same, considering directory separator and OS case sensitivity.

Parameters

string $path1
string $path2

Return Value

bool

makeExecutable(string $file, string $who = 'all')

Try to set the executable bit of a file.

Parameters

string $file

The full path

string $who

One of 'user', 'group', 'all'

Exceptions

Exception

Throws an exception in case of errors