Home > Backend Development > PHP Tutorial > PHP writes or appends data to a file_PHP Tutorial

PHP writes or appends data to a file_PHP Tutorial

WBOY
Release: 2016-07-13 09:57:15
Original
2923 people have browsed it

PHP writes or appends data to files

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"
mode 说明
"r" 只读方式打开,将文件指针指向文件头。
"r " 读写方式打开,将文件指针指向文件头。
"w" 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
"w " 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
"a" 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
"a " 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
"x"

创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。

这和给底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。

此选项被 PHP 4.3.2 以及以后的版本所支持,仅能用于本地文件。

"x "

创建并以读写方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。

这和给底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。

此选项被 PHP 4.3.2 以及以后的版本所支持,仅能用于本地文件。

Create and open for 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.
"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');
Copy after login

(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
Copy after login

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);
Copy after login

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.

Articles you may be interested in

  • The safest and most realistic solution for PHP to determine the type of uploaded files
  • php gets every file within a certain period of time Month method, returns an array composed of these months
  • php program to get all files in a directory and save the results to an array
  • Use PHP's GZip compression function to compress website JS and CSS files Compression accelerates website access speed
  • PHP analyzes the file header information to determine the type of uploaded file
  • Right-click the xp folder to add a new window to open the folder
  • MySQL imports and Example of exporting .sql file backup data operation
  • Solution to PHP failure to upload large files

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/984181.htmlTechArticlePHP writes or appends data to a file. There are two ways to write or append data to a file. One is It is fopen, and the other is file_put_contents. This article briefly introduces the features of the two methods...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template