Home > Article > Backend Development > How to clear file contents in php
In PHP, you can use the file_put_contents() function to clear the file contents. This function can write a string into the file. If the "FILE_APPEND" value is not set, the file contents will be cleared; syntax " file_put_contents("file path","")".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use file_put_contents () function to clear the file contents.
The file_put_contents() function writes a string to a file. The syntax format is:
file_put_contents(file,data,mode,context)
Parameter | Description |
---|---|
file | Required. Specifies the file to which data is to be written. If the file does not exist, a new file is created. |
data | Required. Specifies the data to be written to the file. Can be a string, array, or data stream. |
mode | Optional. Specifies how to open/write the file. Possible values:
|
Optional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream. |
Example: Clear file content
<?php echo file_put_contents("sites.txt",""); ?>Output:
0Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to clear file contents in php. For more information, please follow other related articles on the PHP Chinese website!