教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 解决php中file_get_contents 读取大文件返回false问题

解决php中file_get_contents 读取大文件返回false问题

发布时间:2018-10-24   编辑:jiaochengji.com
教程集为您提供解决php中file,get,contents 读取大文件返回false问题等资源,欢迎您收藏本站,我们将为您提供最新的解决php中file,get,contents 读取大文件返回false问题资源
file_get_contents文件是用来读写文件的,但我发现用file_get_contents 读取大文件出错提示Note: string can be as large as 2GB了,这个就是不能超过2G了,有没有办法解决呢,下面我来一起来看。

如果我读取一个 www.111cn.net文件

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5095')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5095>

$u ='www.111cn.net'; //此文件为100GB

$a =file_get_contents( $u );

运行提示

Note: string can be as large as 2GB

不能大于2GB了,我们去官方看此函数参考


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

发现有个

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


原来如此,这样我们对程序进行修改即可

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1532')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1532>

$u ='www.111cn.net'; //此文件为100GB

$a =file_get_contents( $u,100,1000 );

读取成功了

总结

file_get_contents如果正常返回,会把文件内容储存到某个字符串中,所以它不应该返回超过2G长度的字符串。

如果文件内容超过2G,不加offset和maxlen调用file_get_contents的话,肯定会返回false,

您可能感兴趣的文章:
解决php中file_get_contents 读取大文件返回false问题
PHP file_get_contents超时的设置方法
php中curl、fsocket、file_get_content函数比较
php中file_get_contents超时问题的解决方法
PHP Warning: file_get_contents failed to open stream解决办法
PHP获取(读取)本地文件与远程文件内容示例
php的file_get_contents导致cpu飙升问题的解决方法
php导入phpmyadmin导出的sql的实现代码
php 文件读取与读取文件输出内容例子
php 中file_get_contents超时问题的解决方法

[关闭]
~ ~