File Class
The file class offers a set of methods to working with files & directories. This can be done through
some helper methods or through a more advanced object oriented method where all files and directories are
wrapped in objects.
Usage
The File class's basic usage is through its helper methods which are listed below.
create($basepath, $name, $contents = null, $area = null)
Create a file.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$basepath |
required |
Location to create the new file in |
$name |
required |
Filename for the new file |
$contents |
null |
What to put into the new file |
$area |
null |
Area object to use, defaults to main config (see advanced
usage for more information) |
|
Returns |
true |
Throws |
File_Exception when creation fails |
Example |
File::create(DOCROOT, 'test.txt', 'Contents for file.');
|
create_dir($basepath, $name, $chmod = 0777, $area = null)
Create a directory.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$basepath |
required |
Location to create the new directory in |
$name |
required |
Name for the new directory |
$chmod |
null |
Rights for the new directory |
$area |
null |
Area object to use, defaults to main config (see advanced
usage for more information) |
|
Returns |
bool - the output of the mkdir() PHP function |
Throws |
File_Exception when creation fails |
Example |
File::create(DOCROOT, 'test', 0755);
|
read($path, $as_string = false, $area = null)
Reads a file.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$path |
required |
Location of the file to read |
$as_string |
false |
Makes it use readfile() on false or file_get_contents() on true |
$area |
null |
Area object to use, defaults to main config (see advanced
usage for more information) |
|
Returns |
int|string - returns the number of bytes when $as_string was set to false or the file's contents
as a string when it was set to true. |
Example |
// Returns 'Contents for file.' if you created the file in the example for File::create()
File::read(DOCROOT.'test.txt', true);
|
read_dir($path, $depth = 0, $filter = null, $area = null)
Reads directory contents into an array.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$path |
required |
Location of the directory to read |
$depth |
0 |
How deep the subdirectories are recurred: 0 for unlimited, 1 for only the current
directory or specific number. |
$filter |
null |
A filter for which files to read: array() for all or null for
all but hidden. |
$area |
null |
Area object to use, defaults to main config (see advanced
usage for more information) |
|
Returns |
array - a natural-case sorted array with subdirectories on top. Directories have a string key
with array value (or false when not read because of $depth limitation) and files are int indexed
with filename as value. |
Example |
File::read(DOCROOT);
|
update($basepath, $name, $contents = null, $area = null)
Updates a file.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$basepath |
required |
Location of the file to update |
$name |
required |
Filename for the file. If it does not exist, it will be created |
$contents |
null |
What to put into the file |
$area |
null |
Area object to use, defaults to main config (see advanced
usage for more information) |
|
Returns |
true |
Throws |
File_Exception when creation fails |
Example |
File::update(DOCROOT, 'test.txt', 'New contents for file.');
|
append($basepath, $name, $contents = null, $area = null)
Appends to an existing file.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$basepath |
required |
Location of the file to append to |
$name |
required |
Filename for the file. This file has to exist |
$contents |
null |
What to add to the file |
$area |
null |
Area object to use, defaults to main config (see advanced
usage for more information) |
|
Returns |
true |
Throws |
File_Exception when the file does not exist |
Example |
File::append(DOCROOT, 'test.txt', 'Additional contents for file.');
|