Code Igniter User Guide Version 1.5.0b3


File Helper

The File Helper file contains functions that assist in working with files.

Loading this Helper

This helper is loaded using the following code:

$this->load->helper('file');

The following functions are available:

read_file('path')

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. Code Igniter uses a front controller so paths are always relative to the main site index.

If you 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)

Writes data to the file specified in the path. If the file does not exist 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: In order for this function to write data to a file its file permissions must be set such that it is writable (666, 777, etc.). If the file does not already exist, the directory containing it must be writable.

Note: The path is relative to your main site index.php file, NOT your controller or view files. Code Igniter uses a front controller so paths are always relative to the main site index.

delete_files('path')

Deletes ALL files contained in the supplied path. Example:

delete_files('/path/to/directory/');

If the second parameter is set to true, any directories contained within the supplied root path will be deleted as well. Example:

delete_files('/path/to/directory/', TRUE);

Note: The files must be writable or owned by the system in order to be deleted.

get_filenames('path/to/directory/')

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.