PHP file_put_contents() function
Definition and Usage
The file_put_contents() function writes a string to a file.
Is equivalent to calling fopen(), fwrite(), and fclose() in sequence.
Syntax
file_put_contents(file,data,mode,context)
Parameter | Description |
---|---|
file | Required. Specifies the file to write the data to. If the file does not exist, a new file will be created. |
data | Optional. Specifies the data to be written to the file. It can be a string, an array, or a data stream. |
mode |
Optional. Specifies how to open/write to the file. Possible values:
|
context |
Optional. Specifies the environment for the file handle. context is a set of options that can modify the behavior of the stream. If null is used, it is ignored. |
Description
Parameter data can be an array (but cannot be a multidimensional array).
Since PHP 5.1.0,data The parameter can also be specified as a stream resource, the cached data in the stream will be written to the specified file, which is similar to using the stream_copy_to_stream() function.
for context Parameter support was added in PHP 5.0.0.
Return Value
This function will return the number of bytes written into the file.
Tips and Comments
Tip:Using FILE_APPEND can prevent the deletion of existing content in the file.
Note:This function can be safely used for binary objects.
Example
<?php echo file_put_contents("test.txt","Hello World. Testing!"); ?>
Output:
26