Description

If filename does not exist, the file is created.
Otherwise, the existing file is overwritten, unless the
FILE_APPEND flag is set.

Parameters

filename

Path to the file where to write the data.

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().

You can also specify the data parameter as a single
dimension array. This is equivalent to
file_put_contents($filename, implode('', $array)).

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 proceeding to the
writing. In other words, a flock() call happens
between the fopen() call and the
fwrite() call. This is not identical to an
fopen() call with mode "x".

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\n";// Write the contents back to the filefile_put_contents($file, $current);?>

Example #2 Using flags

<?php$file = 'people.txt';// The new person to add to the file$person = "John Smith\n";// 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 timefile_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() 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.

It should be obvious that this should only be used if you're making one write, if you are writing multiple times to the same file you should handle it yourself with fopen and fwrite, the fclose when you are done writing.

It's worth noting that you must make sure to use the correct path when working with this function. I was using it to help with logging in an error handler and sometimes it would work - while other times it wouldn't. In the end it was because sometimes it was called from different paths resulting in a failure to write to the log file.

file_put_contents does not issue an error message if file name is incorrect(for example has improper symbols on the end of it /n,/t)
that is why use trim() for file name.
$name=trim($name);
file_put_contents($name,$content);

I had some troubles using file_put_contents with an absolute but no canonicalized path (eg. w:/htdocs/pri/../test/log.txt): on windows environment php was unable to create the file also using the realpath function .I had to use fopen and frwite functions to write the data.

I use the following code to create a rudimentary text editor. It's not fancy, but then it doesn't have to be. You could easily add a parameter to specify a file to edit; I have not done so to avoid the potential security headaches.

There are still obvious security holes here, but for most applications it should be reasonably safe if implemented for brief periods in a counterintuitive spot. (Nobody says you have to make a PHP file for that purpose; you can tack it on anywhere, so long as it is at the beginning of a file.)

Then simply browse to hxxp://www.example.com/{$this_file}?{$random1}={$random2}, with the appropriate values substituted for each bracketed variable. Please note that this code assumes the target file to be world writable (-rw-rw-rw- or 666) and will fail to save properly without error if it is not.

Once again, this is by no means secure or permanent, but as a quick fix for brief edits to noncritical files it should be sufficient, and its small size is a definite bonus.

I wrote this script implementing the file_put_contents() and file_get_contents() functions to be compatible with both php4.* and php 5.*. It is a PHP Command line interface script which searches and replaces a specific word recursively through all files in the supplied directory hierarchy.

//Please do not edit below to keep the rights to this script//Free license, if contents below this line is not editedecho "REPLACED\n'$searchFor2' with '$replaceWith2' recursively through directory listed below\nFor all files that current user has write permissions for\nDIRECTORY: '$parentDirectory2'\n";echo "command written by Kolapo Akande :) all rights reserved :)\n";

This php.net example could be confusing:<?phpfile_put_contents($file, $person, FILE_APPEND | LOCK_EX);?>

You do not need LOCK_EX if you write only to a file (like log files). Multiple writing processes are already queued. Test it on your own by calling this script 4 times simultaneously:<?phpfunction string_rand($len, $split="\n") { return substr(chunk_split(bin2hex(openssl_random_pseudo_bytes(ceil($len / 2))), 1023, $split), 0, $len);}