توابع file_put_contents() در PHP
Definition and Usage
The file_put_contents() function writes a string into a file.
It has the same function as calling fopen(), fwrite(), and fclose() sequentially.
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 of the file handle. context 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).
Starting from 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.
to context Support for parameters 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