Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

file_put_contents(3) [php man page]

FILE_PUT_CONTENTS(3)							 1						      FILE_PUT_CONTENTS(3)

file_put_contents - Write a string to a file

SYNOPSIS
int file_put_contents (string $filename, mixed $data, [int $flags], [resource $context]) DESCRIPTION
This function is identical to calling fopen(3), fwrite(3) and fclose(3) successively to write data to a file. If $filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set. PARAMETERS
o $filename - Path to the file where to write the data. o $data - The data to write. Can be either a string, an array or a stream resource. 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(3). You can also specify the $data parameter as a single dimension array. This is equivalent to file_put_contents($filename, implode('', $array)). o $flags - The value of $flags can be any combination of the following flags, joined with the binary OR ( |) operator. Available flags +----------------------+---------------------------------------------------+ | Flag | | | | | | | Description | | | | +----------------------+---------------------------------------------------+ | | | |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. | | | | | | | | LOCK_EX | | | | | | | Acquire an exclusive lock on the file while pro- | | | ceeding to the writing. In other words, a | | | flock(3) call happens between the fopen(3) call | | | and the fwrite(3) call. This is not identical to | | | an fopen(3) call with mode "x". | | | | +----------------------+---------------------------------------------------+ o $context - A valid context resource created with stream_context_create(3). RETURN VALUES
This function returns the number of bytes that were written to the file, or FALSE on failure. Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function. EXAMPLES
Example #1 Simple usage example <?php $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 "; // Write the contents back to the file file_put_contents($file, $current); ?> Example #2 Using flags <?php $file = 'people.txt'; // The new person to add to the file $person = "John Smith "; // Write the contents to the file, // using the FILE_APPEND flag to append the content to the end of the file // and the LOCK_EX flag to prevent anyone else writing to the file at the same time file_put_contents($file, $person, FILE_APPEND | LOCK_EX); ?> CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.1.0 | | | | | | | Added support for LOCK_EX and the ability to | | | pass a stream resource to the $data parameter | | | | +--------+---------------------------------------------------+ NOTES
Note This function is binary-safe. Tip A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen(3) for more details on how to specify the filename. See the "Supported Protocols and 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. SEE ALSO
fopen(3), fwrite(3), file_get_contents(3), stream_context_create(3). PHP Documentation Group FILE_PUT_CONTENTS(3)
Man Page