The file_put_contents() function writes a string to a file.
When this function accesses files, it follows the following rules:
If FILE_USE_INCLUDE_PATH is set, then the built-in path to the copy of *filename* will be checked
If the file does not exist, a file will be created
open file
If LOCK_EX is set, the file will be locked
If FILE_APPEND is set, it will be moved to the end of the file. Otherwise, the contents of the file will be cleared
Write data to file
Close files and unlock all files
If successful, the function returns the number of characters written to the file. If it fails, False is returned.
int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )
parameter | describe |
---|---|
file | Required. Specifies the file to which data is to be written. If the file does not exist, a new file is created. |
data | Required. Specifies the data to be written to the file. Can be a string, array, or data stream. |
mode | Optional. Specifies how to open/write the file. Possible values: FILE_USE_INCLUDE_PATH FILE_APPEND LOCK_EX |
context | Optional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream. |
NOTE: Use FILE_APPEND to avoid deleting content that already exists in the file.
The execution output of the above example is:
6
Next we append content to the file sites.txt:
After successful execution, open the sites.txt file, the content is:
CoderctoGoogle