file_put_contents()
function writes a string into a file.
The same is true for calling fopen()
, fwrite()
and fclose()
functions in turn.
<?php echo file_put_contents ( "test.txt" , "Hello World. Testing!" ) ; ?>
Output:
26
file_put_contents ( file , data , mode , context )
parameter | describe |
---|---|
file | Required. Specifies the file to be written into the data. If the file does not exist, a new file is created. |
data | Optional. Specifies the data to be written to the file. Can be a string, array, or data stream. |
mode |
Optional. Specifies how to open/write files. Possible values:
|
context |
Optional. Specifies the environment for file handles. context is a set of options that can modify the behavior of a stream. If null is used, it will be ignored. |
The parameter data can be an array (but not a multidimensional array).
Since PHP 5.1.0, the data parameter can also be specified as a stream resource. The cached data saved in the stream will be written to the specified file. This usage is similar to using stream_copy_to_stream()
function.
Support for context parameters is added by PHP 5.0.0.
This function returns the number of bytes written to the data in the file.