write_file()

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. CodeIgniter 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.

doc_CodeIgniter
2016-10-15 16:32:47
Comments
Leave a Comment

Please login to continue.