zoukankan      html  css  js  c++  java
  • php中file_get_contents如何读取大容量文件

    php中file_get_contents如何读取大容量文件

    一、总结

    一句话总结:使用file_get_contents()进行分段读取,file_get_contents()函数可以分段读取

    1、读取大文件是,file_get_contents()函数为什么会发生错误?

    发生内存溢出而打开错误

    当我们遇到文本文件体积很大时,比如超过几十M甚至几百M几G的大文件,用记事本或者其它编辑器打开往往不能成功,因为他们都需要把文件内容全部放到内存里面,这时就会发生内存溢出而打开错误

    因为file_get_contents()只能读取长度为 maxlen 的内容

    2、file_get_contents()函数的机制是什么?

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

    3、file_get_contents()函数如何读取大文件?

    使用file_get_contents()进行分段读取

    $str = $content=file_get_contents("2.sql",FALSE,NULL,1024*1024,1024);
    echo $str;

    $u ='www.bKjia.c0m'; //此文件为100GB

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

    读取成功了

    4、fread()函数如何分段读取?

    其实就是判断文件是否结束,没结束就一直读

    $fp=fopen('2.sql','r');
    while (!feof($fp)){
    $str.=fread($fp, filesize ($filename)/10);//每次读出文件10分之1

    5、如何设置file_get_contents函数的超时时间?

    <?php
    //设置超时参数
    $opts=array(
            "http"=>array(
                    "method"=>"GET",
                    "timeout"=>3
                    ),
            );
    ////创建数据流上下文
    $context = stream_context_create($opts);
    
    //$url请求的地址,例如:
    
    $result =file_get_contents($url, false, $context);
    
    // 打印结果
    print_r($result);
    
    ?>

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

    当我们遇到文本文件体积很大时,比如超过几十M甚至几百M几G的大文件,用记事本或者其它编辑器打开往往不能成功,因为他们都需要把文件内容全部放到内存里面,这时就会发生内存溢出而打开错误,遇到这种情况我们可以使用PHP的文件读取函数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() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。

    应用:

    代码如下:

    $str = $content=file_get_contents("2.sql",FALSE,NULL,1024*1024,1024);
    echo $str;

    如果针对较小文件只是希望分段读取并以此读完可以使用fread()函数

    代码如下:

    $fp=fopen('2.sql','r');
    while (!feof($fp)){
    $str.=fread($fp, filesize ($filename)/10);//每次读出文件10分之1
    //进行处理
    }

    echo $str;

    以上就是如何使用file_get_contents函数读取大文件的方法,超级简单吧,需要的小伙伴直接搬走!

    参考:php 使用file_get_contents读取大文件的方法_php技巧_脚本之家
    https://www.jb51.net/article/57380.htm

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

    file_get_contents文件是用来读写文件的,但我发现用file_get_contents 读取大文件出错提示Note: string can be as large as 2GB了,这个就是不能超过2G了,有没有办法解决呢,下面我来一起来看。

    如果我读取一个 www.bKjia.c0m文件

     代码如下
    复制代码

    $u ='www.bKjia.c0m'; //此文件为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。

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

     代码如下
    复制代码

    $u ='www.bKjia.c0m'; //此文件为100GB

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

    读取成功了

    总结

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

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

    参考:解决php中file_get_contents 读取大文件返回false问题 | E网新时代
    http://www.jxtobo.com/56854.html

     
     

    四、PHP中file_get_contents($url)的超时处理

    PHP中file_get_contents函数的作用是获取一个 URL 的返回内容。如果是url响应速度慢,或者网络等因素,会造成等待时间较长的情况。只需设置一下file_get_contents函数的超时时间即可解决。示例代码如下:

    <?php
    //设置超时参数
    $opts=array(
            "http"=>array(
                    "method"=>"GET",
                    "timeout"=>3
                    ),
            );
    ////创建数据流上下文
    $context = stream_context_create($opts);
    
    //$url请求的地址,例如:
    
    $result =file_get_contents($url, false, $context);
    
    // 打印结果
    print_r($result);
    
    ?>

    参考:PHP中file_get_contents($url)的超时处理 - CSDN博客
    https://blog.csdn.net/mayuko2012/article/details/76092312

     
  • 相关阅读:
    任意不规则形状的图片剪裁 .
    C#图片处理示例(裁剪,缩放,清晰度,水印)
    C#对图片文件的压缩、裁剪操作初探
    CSS和SVG中的剪切——clip-path属性和<clipPath>元素
    Java中继承thread类与实现Runnable接口的区别
    Android学习系列(7)--App轮询服务器消息
    Android学习系列(15)--App列表之游标ListView(索引ListView)
    Android学习系列(20)--App数据格式之解析Json
    Android学习系列(22)--App主界面比较
    Android学习系列(23)--App主界面实现
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/9642301.html
Copyright © 2011-2022 走看看