打印本文 打印本文 关闭窗口 关闭窗口
file_get_contents()的用法— 将整个文件读入一个字符串
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1837  更新时间:2010/3/22 22:32:53  文章录入:mintao  责任编辑:mintao

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

说明
string file_get_contents ( string $filename [, bool $use_include_path [, resource $context [, int $offset [, int $maxlen ]]]] )
和 file() 一样,只除了 file_get_contents() 把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。如果失败,file_get_contents() 将返回 FALSE。

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

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

Note: context 参数可以用 NULL 来忽略。

语法

file_get_contents(path,include_path,context,start,max_length)

参数 描述
path 必需。规定要读取的文件。
include_path 可选。如果也想在 include_path 中搜寻文件的话,可以将该参数设为 "1"。
context

可选。规定文件句柄的环境。

context 是一套可以修改流的行为的选项。若使用 null,则忽略。

start 可选。规定在文件中开始读取的位置。该参数是 PHP 5.1 新加的。
max_length 可选。规定读取的字节数。该参数是 PHP 5.1 新加的。

说明

context 的支持是 PHP 5.0.0 添加的。

提示和注释

注释:本函数可安全用于二进制对象。

例子

<?php
print_r(file("test.txt"));
?>

输出:

Array
(
[0] => Hello World. Testing testing!
[1] => Another day, another line.
[2] => If the array picks up this line,
[3] => then is it a pickup line?
)
打印本文 打印本文 关闭窗口 关闭窗口