The file_put_contents() writes a string to a file.
int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )
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.
This function follows these rules when accessing a file:
This function returns the number of character written into the file on success, or FALSE on failure.
Parameter | Description |
---|---|
file | Required. Specifies the file to write to. If the file does not exist, this function will create one |
data | Required. The data to write to the file. Can be a string, an array or a data stream |
mode | Optional. Specifies how to open/write to the file. Possible values: |
FILE_USE_INCLUDE_PATH | |
FILE_APPEND | |
LOCK_EX | |
context | Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream. |
Use FILE_APPEND to avoid deleting the existing content of the file.