file_put_contents函数常用方法

1、file_put_contents

(PHP 5, PHP 7)

file_put_contents 将一个字符串写入文件

int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )


和依次调用 fopen()fwrite() 以及 fclose() 功能一样。

If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set. 

(如果文件不存在将会自动创建)

参数

filename

要被写入数据的文件名。

data

要写入的数据。类型可以是 stringarray 或者是 stream 资源(如上面所说的那样)。

如果 data 指定为 stream 资源,这里 stream 中所保存的缓存数据将被写入到指定文件中,这种用法就相似于使用 stream_copy_to_stream() 函数。

参数 data 可以是数组(但不能为多维数组),这就相当于 file_put_contents($filename, join('', $array))

flags

flags 的值可以是 以下 flag 使用 OR (|) 运算符进行的组合。

Available flags
Flag描述
FILE_USE_INCLUDE_PATH在 include 目录里搜索 filename。 更多信息可参见 include_path
FILE_APPEND如果文件 filename 已经存在,追加数据而不是覆盖。
LOCK_EX在写入时获得一个独占锁。

context

一个 context 资源。

返回值

该函数将返回写入到文件内数据的字节数,失败时返回FALSE

Warning

此函数可能返回布尔值 FALSE,但也可能返回等同于 FALSE 的非布尔值。请阅读 布尔类型章节以获取更多信息。应使用 === 运算符来测试此函数的返回值。



2、

file_get_contents


(PHP 4 >= 4.3.0, PHP 5, PHP 7)

file_get_contents 将整个文件读入一个字符串

说明

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )

file() 一样,只除了 file_get_contents() 把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。如果失败,file_get_contents() 将返回 FALSE

file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。

Note:

如果要打开有特殊字符的 URL (比如说有空格),就需要使用 urlencode() 进行 URL 编码。

参数

filename

要读取的文件的名称。

use_include_path

Note:

As of PHP 5 the FILE_USE_INCLUDE_PATH can be used to trigger include path search.

context

A valid context resource created with stream_context_create(). 如果你不需要自定义 context,可以用 NULL 来忽略。

offset

The offset where the reading starts on the original stream.

Seeking (offset) is not supported with remote files. Attempting to seek on non-local files may work with small offsets, but this is unpredictable because it works on the buffered stream.

maxlen

Maximum length of data read. The default is to read until end of file is reached. Note that this parameter is applied to the stream processed by the filters.

返回值

The function returns the read data 或者在失败时返回 FALSE



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值