Write data to a file.
<?php echo file_put_contents(“test.txt”,“Hello World. Testing!”); ?> |
The result of the code above will be:
21 |
The file_put_contents() function writes data to a file and operates as follows:
If FILE_USE_INCLUDE_PATH is set, it searches for the file in the include path.
Note: Use FILE_APPEND to prevent deleting existing content in the file.
file_put_contents(filename, data, mode, context) |
Parameter |
Description |
filename |
Mandatory. Specifies the path to the file to write to. If the file does not exist, it will be created. |
data |
Mandatory. The data to write to the file, which can be a string, array, or data stream. |
mode |
Optional. Specifies how to open or write to the file. Possible values include:
|
context |
Optional. Specifies the context for the file handle, which is a set of options that can alter the behavior of the stream. |
Return Value: |
Returns the number of bytes written to the file on success, or |
PHP Version: |
5.0+ |
Binary Safe: |
Yes |
PHP Changelog: |
In PHP 5.1, support was added for |