File Helper¶
The File Helper file contains functions that assist in working with files.
Available Functions¶
The following functions are available:
-
read_file
($file)¶ -
Parameters: - $file (string) – File path
Returns: File contents or FALSE on failure
Return type: string
Returns the data contained in the file specified in the path.
Example:
$string = read_file('./path/to/file.php');
The path can be a relative or full server path. Returns FALSE (boolean) on failure.
Note
The path is relative to your main site index.php file, NOT your controller or view files. Codingox uses a front controller so paths are always relative to the main site index.
Note
This function is DEPRECATED. Use the native
file_get_contents()
instead.Important
If your server is running an open_basedir restriction this function might not work if you are trying to access a file above the calling script.
-
write_file
($path, $data, $mode = 'wb')¶ -
Parameters: - $path (string) – File path
- $data (string) – Data to write to file
- $mode (string) –
fopen()
mode
Returns: TRUE if the write was successful, FALSE in case of an error
Return type: bool
Writes data to the file specified in the path. If the file does not exist then the function will create it.
Example:
$data = 'Some file data'; if ( ! write_file('./path/to/file.php', $data)) { echo 'Unable to write the file'; } else { echo 'File written!'; }
You can optionally set the write mode via the third parameter:
write_file('./path/to/file.php', $data, 'r+');
The default mode is ‘wb’. Please see the PHP user guide for mode options.
Note
The path is relative to your main site index.php file, NOT your controller or view files. Codingox uses a front controller so paths are always relative to the main site index.
Note
This function acquires an exclusive lock on the file while writing to it.
-
delete_file
($path)¶ -
Parameters: - $path (string) – Directory path
Returns: TRUE on success, FALSE in case of an error
Return type: bool
Example:
delete_file('./path/to/directory/');
Note
The files must be writable or owned by the system in order to be deleted.
-
get_filename
($source_dir, $include_path = FALSE)¶ -
Parameters: - $source_dir (string) – Directory path
- $include_path (bool) – Whether to include the path as part of the filenames
Returns: An array of file names
Return type: array
Takes a server path as input and returns an array containing the names of all files contained within it. The file path can optionally be added to the file names by setting the second parameter to TRUE.
Example:
$controllers = get_filenames(APP_PATH.'controllers/');
-
get_file_info
($file, $returned_values = array('name', 'server_path', 'size', 'date'))¶ -
Parameters: - $file (string) – File path
- $returned_values (array) – What type of info to return
Returns: An array containing info on the specified file or FALSE on failure
Return type: array
Given a file and path, returns (optionally) the name, path, size and date modified information attributes for a file. Second parameter allows you to explicitly declare what information you want returned.
Valid
$returned_values
options are: name, size, date, readable, writeable, executable and fileperms.