Definition and Usage
定义和用法
The file_put_contents() writes a string to a file.
file_put_contents()函数的作用是:将一个字符串写入文件。
This function follows these rules when accessing a file:
当访问一个文件是,函数必须遵循下面这些法则:
If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of *filename*
如果设置了FILE_USE_INCLUDE_PATH,那么将检查*filename*副本的内置路径
Create the file if it does not exist
如果文件不存在,将创建一个文件
Open the file
打开文件
Lock the file if LOCK_EX is set
如果设置了LOCK_EX,那将对文件上锁
If FILE_APPEND is set, move to the end of the file. Otherwise, clear the file content
如果设置了FILE_APPEND,那么将移至文件末尾;否则的话,将会清除文件的内容
Write the data into the file
向文件中写入数据
Close the file and release any locks
关闭文件并对所有文件解锁
This function returns the number of character written into the file on success, or FALSE on failure.
如果函数运行成功,将返回写入文件中的字符数量;如果失败,则返回False。
Syntax
语法
file_put_contents(file,data,mode,context)
Parameter
参数Description
描述
file
Required. Specifies the file to write to. If the file does not exist, this function will create one
必要参数。指定写入文件对象。如果该文件不存在,函数将自动创建该文件
data
Required. The data to write to the file. Can be a string, an array or a data stream
必要参数。指定写入文件的数据,可以使一个字符串或者是一个数组
mode
Optional. Specifies how to open/write to the file. Possible values:
可选参数。指定打开/书写文件的方法:
FILE_USE_INCLUDE_PATH
FILE_APPEND
LOCK_EX
context
Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream.
可选参数。指定需要进行处理的文件context[内容]。Context是一组选项,你可以通过它修改你的文本属性
Tips and Notes
注意点 Note: Use FILE_APPEND to avoid deleting the existing content of the file.
注意:使用FILE_APPEND避免删除文件的现存内容。