`

PHP file_get_contents() 函数

    博客分类:
  • PHP
阅读更多

定义和用法

file_get_contents() 函数把整个文件读入一个字符串中。

和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。

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

语法

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?
)
分享到:
评论

相关推荐

    PHP file_get_contents 函数超时的几种解决方法

    这里就简单介绍两种: 一、增加超时的时间限制 这里需要注意:set_time_limit只是设置你的PHP程序的超时时间,而不是file_get_contents函数读取URL的超时时间。 我一开始以为set_time_limit也能影响到file_get_...

    PHP中file_get_contents函数抓取https地址出错的解决方法(两种方法)

    [function.file-get-contents]: failed to open stream: Invalid argument in I:Webmyphpa.php on line 16 打开php.ini文件找到 ;extension=php_openssl.dll ,去掉双引号”;” ,重启web服务器即可。 apache服务器...

    关于file_get_contents返回为空或函数不可用的解决方案

    如果你使用file_get_contents获取远程文件内容返回为空或提示该函数不可用,也许本文能帮到你! 使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置allow_url_fopen = On,allow_url...

    深入file_get_contents函数抓取内容失败的原因分析

    下面是file_get_contents和curl两个函数同样功能的不同写法file_get_contents函数的使用示例:复制代码 代码如下:&lt; ?php$file_contents = file_get_contents&#40;‘https://www.jb51.net’&#41;;echo $file_...

    解决file_get_contents无法请求https连接的方法

    错误: Warning: fopen&#40;&#41; [function.fopen]: Unable to find the wrapper “https” – did you forget to enable it ...3.如果服务器你不能修改配置的话,那么就使用curl函数来替代file_get_contents函数,

    PHP file_get_contents设置超时处理方法

    file_get_contents的超时处理 话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。 今天说的这...

    深入file_get_contents与curl函数的详解

    下面是file_get_contents和curl两个函数同样功能的不同写法file_get_contents函数的使用示例:复制代码 代码如下:&lt; ?php$file_contents = file_get_contents&#40;‘https://www.jb51.net’&#41;;echo $file_...

    php file_get_contents函数轻松采集html数据

    $html = file_get_contents&#40;$page,’r’&#41;; $pattern=”/全国主要城市、县当天和未来五天天气趋势预报在线查询&lt;\/B&gt;(.*?)&lt;center xss=removed&gt;/si”; //正则匹配之间的html preg_match($pattern,$html,$...

    PHP file_get_contents函数读取远程数据超时的解决方法

    在网络状况比较差的情况下file_get_contents函数经常读取远程数据失败。 解决办法如下: 复制代码 代码如下: /*设置超时配合失败之后尝试多次读取,效果比原先好很多*/ $url = ‘https://www.jb51.net’;  $opts = ...

    php 使用file_get_contents读取大文件的方法

    本文介绍了在php中使用file_get_contents函数读取大文件的方法,并附上了示例以及使用小技巧,非常的实用,这里推荐给大家

    比file_get_contents稳定的curl_get_contents分享

    分享一个实际在用的函数: 复制代码 代码如下: /*比file_get_contents稳定的多!$timeout为超时时间,单位是秒,默认为1s。*/ function curl_get_contents($url,$timeout=1) { $curlHandle = curl_init(); curl_...

    深入php函数file_get_contents超时处理的方法详解

    增加超时的时间限制 这里需要注意:set_time_limit只是设置你的PHP程序的超时时间,而不是file_get_contents函数读取URL的超时时间。真正的修改 file_get_contents延时可以用resource $context的timeout参数: 复制...

    php基于curl重写file_get_contents函数实例

    本文实例讲述了php基于curl重写file_get_contents函数。分享给大家供大家参考,具体如下: file_get_contents在连接不上的时候会提示Connection refused,有时候会带来不便;另外,curl的性能比file_get_contents高...

    PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析

    后来,我通过跟踪发现,这类情况的出现,跟 PHP 的 file_get_contents&#40;&#41; 函数有着密切的关系。 大、中型网站中,基于 HTTP 协议的 API 接口调用,是家常便饭。PHP 程序员们喜欢使用简单便捷的 file_get_...

    php中file_get_contents与curl性能比较分析

    本文实例讲述了php中file...PHP中fopen,file_get_contents,curl函数的区别: 1.fopen /file_get_contents 每次请求都会重新做DNS查询,并不对 DNS信息进行缓存。但是CURL会自动对DNS信息进行缓存。对同一域名下的网页或

Global site tag (gtag.js) - Google Analytics