Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

file_put_contents

Description

The file_put_contents of Filesystem for PHP write data to a file.

Syntax

file_put_contents(
    string $filename,
    mixed $data,
    int $flags = 0,
    ?resource $context = null
): int|false

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.

ValueNameDescription
1FILE_USE_INCLUDE_PATHSearch for filename in the include directory. See include_path for more information.
2LOCK_EXAcquire 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".
8FILE_APPENDIf 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 === operator for testing the return value of this function.

Examples

1 · filename data

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/1.txt";
$data = "hello";

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

echo $return;

?>
5

2 · flags · 0

<?

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

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

echo $return;

?>
5

3 · flags · LOCK_EX

<?

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

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

echo $return;

?>
5

4 · flags · FILE_APPEND

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/1.txt";
$data = " world";
$flags = FILE_APPEND;

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

echo $return;

?>
6

5 · context

<?

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

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

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

echo $return;

?>
5

6 · Append · file_get_contents

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/1.txt";
$data = file_get_contents($filename);
$data .= " world";

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

echo $return;

?>
11

7 · Append · FILE_APPEND

<?

$filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/1.txt";
$data = " world";
$flags = FILE_APPEND | LOCK_EX;

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

echo $return;

?>
6
HomeMenu