Menu Riverview Church

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

5

FLAGS | 0

5

FLAGS | LOCK_EX

5

FLAGS | FILE_APPEND

7

CONTEXT

5

APPEND | FILE_GET_CONTENTS

12

APPEND | FILE_APPEND

7

INTERNAL

file_get_contents

EXTERNAL

PHP