Home > Article > Backend Development > PHP writes or appends data to a file_PHP Tutorial
There are two ways for PHP to write or append data to files, one is fopen and the other is file_put_contents. This article is brief Let me introduce the specific usage of the two methods. Friends in need can take a look.
(1)fopen
The fopen() function opens a file or URL. If the opening fails, this function returns FALSE.
Syntax: fopen(filename,mode,include_path,context)
Parameter Description
filename Required. Specifies the file or URL to open.
mode Required. Specifies the type of access required to this file/stream. Possible values are shown in the table below.
include_path is optional. If you also need to retrieve files in include_path, you can set this parameter to 1 or TRUE.
context Optional. Specifies the environment for a file handle. Context is a set of options that can modify the behavior of the stream.
Possible values for the mode parameter
mode | Description | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
"r" | Open in read-only mode and point the file pointer to the file header. | ||||||||||||||||||
"r " | Open in read-write mode and point the file pointer to the file header. | ||||||||||||||||||
"w" | Open in writing mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it. | ||||||||||||||||||
"w " | Open in read-write mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it. | ||||||||||||||||||
"a" | Open in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it. | ||||||||||||||||||
"a " | Open in read-write mode and point the file pointer to the end of the file. If the file does not exist, try to create it. | ||||||||||||||||||
"x" |
|
||||||||||||||||||
"x " | Create and open for reading and writing, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE and generates an E_WARNING level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL|O_CREAT flag to the underlying open(2) system call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files. |
Write content in append form
<?php $fp=fopen('test.txt','a');
(2) file_put_contents
The file_put_contents() function is used to write a string into a file. It returns the number of bytes of data written into the file successfully, and returns FALSE on failure.
Syntax: int file_put_contents(string filename,string data[,int flags[,resource context]])
Parameter Description
filename The name of the file to write data to
data The data to be written. The type can be string, array (but not multi-dimensional array), or stream resource
flags optional, specifies how to open/write the file. Possible values:
FILE_USE_INCLUDE_PATH: Check the built-in path for a copy of filename
FILE_APPEND: Write data by appending to the end of the file
LOCK_EX: Lock the file
context Optional, Context is a set of options through which text attributes can be modified
For example:
<?php echo file_put_contents("test.txt","www.phpernote.com"); //输出:17
Write content in append form
When the flags parameter value is set to FILE_APPEND, it means writing new data by appending content after the existing file content, for example:
<?php file_put_contents("test.txt","www.phpernote.com",FILE_APPEND);
Tips
The behavior of file_put_contents() is actually equivalent to calling the fopen(), fwrite() and fclose() functions in sequence.
If the file does not exist, create the file, which is equivalent to the fopen() function behavior.
If the file exists, the contents of the file will be cleared by default. You can set the flags parameter value to FILE_APPEND to avoid this.
The file_put_contents function is safe for use with binary objects.