当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > file_put_contents() 函数

PHP
php 无限分类的实现
常用PHP代码
windows下安装配置php视频教程
MySQL数据库结构和数据的导出和导入
PHP实现 IP Whois 查询
PHP5 this,self和parent关键字详解
PHP 安全技巧连载 #1[译]
PHP 安全技巧连载 #2[译]
PHP 安全技巧连载 #3[译]
PHP 安全技巧连载 #4[译]
PHP 安全技巧连载 #5[译]
PHP 安全技巧连载 #6[译]
PHP 安全技巧连载 #7[译]
PHP 安全技巧连载 #8[译]
PHP 安全技巧连载 #9[译]
PHP 安全技巧连载 #10[译]
PHP 安全技巧连载 #11[译]
PHP error_reporting的使用
PHP 安全技巧连载 #12
使用PHP做Linux/Unix守护进程

PHP 中的 file_put_contents() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 719 ::
收藏到网摘: n/a

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:
当访问一个文件是,函数必须遵循下面这些法则:

  1. If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of *filename*
    如果设置了FILE_USE_INCLUDE_PATH,那么将检查*filename*副本的内置路径
  2. Create the file if it does not exist
    如果文件不存在,将创建一个文件
  3. Open the file
    打开文件
  4. Lock the file if LOCK_EX is set
    如果设置了LOCK_EX,那将对文件上锁
  5. If FILE_APPEND is set, move to the end of the file. Otherwise, clear the file content
    如果设置了FILE_APPEND,那么将移至文件末尾;否则的话,将会清除文件的内容
  6. Write the data into the file
    向文件中写入数据
  7. 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避免删除文件的现存内容。


Example
案例

<?php
echo file_put_contents("test.txt","Hello World. Testing!");
?> 

The output of the code above will be:
上述代码将输出下面的结果:

21

评论 (0) All

登陆 | 还没注册?