位置: 编程技术 - 正文

PHP:file_get_contents()的用法_Filesystem函数

编辑:rootadmin
file_get_contents

推荐整理分享PHP:file_get_contents()的用法_Filesystem函数,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

(PHP 4 >= 4.3.0, PHP 5)

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 PHP:file_get_contents()的用法_Filesystem函数

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.

错误/异常

An E_WARNING level error is generated if either maxlength is less than zero, or if seeking to the specified offset in the stream fails.

范例

Example #1 Get and output the source of the homepage of a website

<?php$homepage=file_get_contents('

Example #2 Searching within the include_path

<?php//<=PHP5$file=file_get_contents('./people.txt',true);//>PHP5$file=file_get_contents('./people.txt',FILE_USE_INCLUDE_PATH);?>

Example #3 Reading a section of a file

<?php//Readcharactersstartingfromthestcharacter$section=file_get_contents('./people.txt',NULL,NULL,,);var_dump($section);?>

以上例程的输出类似于:

Example #4 Using stream contexts

<?php//Createastream$opts=array('http'=>array('method'=>"GET",'header'=>"Accept-language:enrn"."Cookie:foo=barrn"));$context=stream_context_create($opts);//OpenthefileusingtheHTTPheaderssetabove$file=file_get_contents('

更新日志

版本 说明 5.1.0 Added the offset and maxlen parameters. 5.0.0 Added context support.

注释

Note: 此函数可安全用于二进制对象。

Tip

如已启用fopen 包装器,在此函数中, URL 可作为文件名。关于如何指定文件名详见 fopen()。各种wapper 的不同功能请参见 支持的协议和封装协议,注意其用法及其可提供的预定义变量。

Warning

使用 SSL 时,Microsoft IIS会违反协议不发送close_notify标记就关闭连接。PHP 会在到达数据尾端时报告“SSL: Fatal Protocol Error”。要解决此问题,error_reporting 应设定为降低级别至不包含警告。PHP 4.3.7 及更高版本可以在使用 包装器打开流时检测出有问题的 IIS 服务器软件 并抑制警告。在使用fsockopen() 创建 套接字时, 开发者需检测并抑制此警告。

参见

file() - 把整个文件读入一个数组中 fgets() - 从文件指针中读取一行 fread() - 读取文件(可安全用于二进制文件) readfile() - 输出一个文件 file_put_contents() - 将一个字符串写入文件 stream_get_contents() - 读取资源流到一个字符串 stream_context_create() - 创建资源流上下文 $http_response_header

PHP:fgets()的用法_Filesystem函数 fgets(PHP4,PHP5)fgets从文件指针中读取一行说明stringfgets(resource$handle[,int$length])从文件指针中读取一行。参数handle文件指针必须是有效的,必须指向由fopen()

PHP:fgetss()的用法_Filesystem函数 fgetss(PHP4,PHP5)fgetss从文件指针中读取一行并过滤掉HTML标记说明stringfgetss(resource$handle[,int$length[,string$allowable_tags]])和fgets()相同,只除了fgetss()尝试从读取

PHP:feof()的用法_Filesystem函数 feof(PHP4,PHP5)feof测试文件指针是否到了文件结束的位置说明boolfeof(resource$handle)测试文件指针是否到了文件结束的位。参数handle文件指针必须是有效的,必

本文链接地址:https://www.jiuchutong.com/biancheng/285231.html 转载请保留说明!

上一篇:PHP:file()的用法_Filesystem函数(php中file)

下一篇:PHP:fgets()的用法_Filesystem函数(php foo)

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络