码迷,mamicode.com
首页 > Web开发 > 详细

PHP 文件写入或追加数据

时间:2014-07-16 18:22:36      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   strong   文件   os   

PHP file_put_contents() 函数是一次性向文件写入字符串或追加字符串内容的最合适选择。

file_put_contents()

file_put_contents() 函数用于把字符串写入文件,成功返回写入到文件内数据的字节数,失败则返回 FALSE。

语法:

int file_put_contents ( string filename, string data [, int flags [, resource context]] )
参数说明:
参数说明
filename 要写入数据的文件名
data 要写入的数据。类型可以是 string,array(但不能为多维数组),或者是 stream 资源
flags 可选,规定如何打开/写入文件。可能的值:
  1. FILE_USE_INCLUDE_PATH:检查 filename 副本的内置路径
  2. FILE_APPEND:在文件末尾以追加的方式写入数据
  3. LOCK_EX:对文件上锁
context 可选,Context是一组选项,可以通过它修改文本属性

例子:

<?php
    echo file_put_contents("test.txt", "This is something.");
?>

运行该例子,浏览器输出:

18

而 test.txt 文件(与程序同目录下)内容则为:This is something.。

提示

  • 如果文件不存在,则创建文件,相当于fopen()函数行为。
  • 如果文件存在,默认将清空文件内的内容,可设置 flags 参数值为 FILE_APPEND 以避免(见下)。
  • 本函数可安全用于二进制对象。

以追加形式写入内容

当设置 flags 参数值为 FILE_APPEND 时,表示在已有文件内容后面追加内容的方式写入新数据:

<?php
    file_put_contents("test.txt", "This is another something.", FILE_APPEND);
?>

执行程序后,test.txt 文件内容变为:This is something.This is another something.

file_put_contents() 的行为实际上等于依次调用 fopen(),fwrite() 以及 fclose() 功能一样。

 

 

 

PHP 文件写入或追加数据,布布扣,bubuko.com

PHP 文件写入或追加数据

标签:style   blog   color   strong   文件   os   

原文地址:http://www.cnblogs.com/phpfensi/p/3845305.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!