file_put_contents — Write a string to a file
If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flags is set.
Path to the file where to write the data.
If data is a stream resource, the remaining buffer of that stream will be copied to the specified file. This is similar with using stream_copy_to_stream().
You can also specify the data parameter as a single dimension array. This is equivalent to file_put_contents($filename, implode('', $array)).
The value of flags can be any combination of the following flags (with some restrictions), joined with the binary OR (|) operator.
|FILE_USE_INCLUDE_PATH||Search for filename in the include directory. See include_path for more information.|
|FILE_APPEND||If file filename already exists, append the data to the file instead of overwriting it. Mutually exclusive with LOCK_EX since appends are atomic and thus there is no reason to lock.|
|LOCK_EX||Acquire an exclusive lock on the file while proceeding to the writing. Mutually exclusive with FILE_APPEND.|
A valid context resource created with stream_context_create().
The function returns the number of bytes that were written to the file, or FALSE on failure.
Example #1 Simple usage example
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
Example #2 Using flags
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Append the contents of $person to the file named by $file.
file_put_contents($file, $person, FILE_APPEND);
|5.0.0||Added context support|
|5.1.0||Added support for LOCK_EX and the ability to pass a stream resource to the data parameter|
Note: This function is binary-safe.
A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the List of Supported Protocols/Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.