How to use php file_put_contents
The file_put_contents function is used to write data to a file.
Its basic syntax is as follows:
file_put_contents(filename, data, flags, context)
Explanation of parameters:
- File name: the name of the file to write data into.
- Data: Information to be written to the file.
- Flags (optional): Specify the file writing mode, with a default value of 0 indicating data is overwritten. If set to FILE_APPEND, data is appended instead.
- context (optional): used to specify the context.
The sample code is shown below:
$file = 'example.txt';
$data = 'Hello, World!';
file_put_contents($file, $data);
The code above writes the string “Hello, World!” to the file example.txt.
If you want to write data in an appending manner, you can use the FILE_APPEND flag.
$file = 'example.txt';
$data = 'Hello, World!';
file_put_contents($file, $data, FILE_APPEND);
This way, each time the file_put_contents function is called, the data will be appended to the end of the file instead of overwriting the existing data.
I hope this helps you!