file_put_contents

Write data to a file

Syntax

file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] ) : int

This function is identical to calling fopen(), fwrite() and fclose() 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

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.

Number Name Description
1 FILE_USE_INCLUDE_PATH Search for filename in the include directory. See include_path for more information.
2 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".
8 FILE_APPEND If file filename already exists, append the data to the file instead of overwriting it.
context

A valid context resource created with stream_context_create().

Return

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 identity operator (===) for testing the return value of this function.

Examples

filename data

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/file.txt";
$data = "Hello";

$return = file_put_contents($filename, $data);

echo $return;

?>
5

flags | 0

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/file.txt";
$data = "Hello";
$flags = 0;

$return = file_put_contents($filename, $data, $flags);

echo $return;

?>
5

flags | LOCK_EX

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/file.txt";
$data = "Hello";
$flags = LOCK_EX;

$return = file_put_contents($filename, $data, $flags);

echo $return;

?>
5

flags | FILE_APPEND

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/file.txt";
$data = " World!";
$flags = FILE_APPEND;

$return = file_put_contents($filename, $data, $flags);

echo $return;

?>
7

context

<?

$options = array("http" => array("method" => "POST"));

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/file.txt";
$data = "Hello";
$flags = 0;
$context = stream_context_create($options);

$return = file_put_contents($filename, $data, $flags, $context);

echo $return;

?>
5

Append | file_get_contents

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/file.txt";
$data = file_get_contents($filename);
$data .= " World!";

$return = file_put_contents($filename, $data);

echo $return;

?>
12

Append | FILE_APPEND

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/file.txt";
$data = " World!";
$flags = FILE_APPEND | LOCK_EX;

$return = file_put_contents($filename, $data);

echo $return;

?>
7

Internal

file_get_contents

External

PHP

Home Menu