php专区

 首页 > php专区 > PHP高级 > 文件上传 > 解决php中file_get_contents 读取大文件返回false问题

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

分享到:
【字体:
导读:
          file_get_contents文件是用来读写文件的,但我发现用file_get_contents 读取大文件出错提示Note: string can be as large as 2GB了...

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

file_get_contents文件是用来读写文件的,但我发现用file_get_contents 读取大文件出错提示Note: string can be as large as 2GB了,这个就是不能超过2G了,有没有办法解决呢?如果我读取一个 www.phpfensi.com 文件,代码如下:

  1. $u ='www.phpfensi.com'//此文件为100GB 
  2. $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。

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

  1. $u ='www.phpfensi.com'//此文件为100GB 
  2. $a =file_get_contents$u,100,1000 ); 

读取成功了,总结:

file_get_contents如果正常返回,会把文件内容储存到某个字符串中,所以它不应该返回超过2G长度的字符串,如果文件内容超过2G,不加offset和maxlen调用file_get_contents的话,肯定会返回false.

分享到:
解决file_get_contents遇到中文文件名无...
解决file_get_contents遇到中文文件名无法打开问题 利用file_get_contents打开文件或采集远程服务器文件如果文名或url中碰到汉字中文那么会出现failed to open stream:Lnvalid argument in错误。 今天遇到一个很棘手的问题,客户用ftp向服务器传了一个文件,但是却无法被程序识别.查看代码后,...
PHP中读写文件 - php文件操作
PHP中读写文件 在php中读写文件有二个比较实用的函数 fopen fwrite函数,有它们两就可以很好的对文件进行操作了,有需要的可以看看详解。 1.fopen(创建文件和打开文件) 语法:fopen(filename,mode)filename,规定要打开的文件。mode,打开文件的模式,可能的值见下表。 mode 说明 "r" ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……