PHP

PHP数据导出超时、内存不足的解决

字号+ 编辑: 国内TP粉 修订: 种花家 来源: ThinkPHP 2023-09-11 我要说两句(1)

当PHP导出数据量过大的时候,经常会遇到超时和内存溢出的解决。

实际工作中,我们经常导出报表的需求,当导出数据量过大的时候,经常会遇到超时和内存溢出的问题。


解决方案一

超时可用: set_time_limit(0) 解决。
内存溢出可用: ini_set('memory_limit', '自定义内存')。

解决方案二

优化程序,利用数据库或文件来缓存中间结果。

解决方案三

利用Ajax分多次请求,写入文件,下载文件,效果如上图(效果图为gif,无法上传,请用下面链接查看)。

(方案三)为大家提供一个Demo

设计思路

1. 我们将其拆成100次请求,请求成功,进度条前进1%。
2. 每次请求都需要写入文件,然后对文件进行追加写入。
3. 当文件写入完毕后,显示下载按钮 ,点击下载即可。

功能点

1. 两种进度条样式。
2. Jquery Ajax。
3. 数据写入CSV。
4. 下载文件。

页面样式:Bootstrap。

代码如下:

<?php
    /**
     * 导出CSV文件
     * @param array  $data                  数据
     * @param array  $header_data 首行数据
     * @param string $file_name         文件名称
     * @param int      $type                    类别
     * @return string
     */
    function _export_csv($data = [], $header_data = [], $file_name = '', $type = 0)
    {
        $fp = fopen($file_name, 'a+');
        if (($type != 1) && !empty($header_data)) {
            foreach ($header_data as $key => $value) {
                $header_data[$key] = iconv('utf-8', 'gbk', $value);
            }
            fputcsv($fp, $header_data);
        }
        $num = 0;
        //每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
        $limit = 100000;
        //逐行取出数据,不浪费内存
        $count = count($data);
        if ($count > 0) {
            for ($i = 0; $i < $count; $i++) {
                $num++;
                //刷新一下输出buffer,防止由于数据过多造成问题
                if ($limit == $num) {
                    ob_flush();
                    flush();
                    $num = 0;
                }
                $row = $data[$i];
                foreach ($row as $key => $value) {
                    $row[$key] = iconv('utf-8', 'gbk', $value);
                }
                fputcsv($fp, $row);
            }
        }
        fclose($fp);
    }
    /**
     * 下载文件
     * @param string $file_url 文件地址
     * @return string
     */
    function _download_file ($file_url = '')
    {
        if (!isset($file_url) || trim($file_url)=='') {
            die('File URL is empty.');
        }
        if (!file_exists($file_url)) {
            die('File does not exist.');
        }
        $file_name = 'down_'.date('YmdHis', time());
        $file_type = fopen($file_url,'r'); //打开文件
        //输入文件标签
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length: ".filesize($file_url));
        header("Content-Disposition: attachment; filename=".$file_name);
        //输出文件内容
        echo fread($file_type, filesize($file_url));
        fclose($file_type);
    }
    //以后是逻辑代码,大家可以根据自己的需求进行编写。
        $path = '文件的绝对地址'; //path 是存放文件的绝对地址。
    if (isset($_POST['start'])) {
          //每一个单独的请求,要保证文件名是唯一的,因为后面要继续进行追加。
              $file_name = 'demo.csv';
        //获取数据,根据自己的业务逻辑,去数据库获取数据。
        $data        = [];
        $header_data = ['执行时间', '随机数']; //首行数据,表头
        //模拟数据如下:
        for ($i=0; $i<=100; $i++) {
            $data[$i]['time'] = date('Y-m-d H:i:s', time());
            $data[$i]['num']  = mt_rand(1000,9999);
        }
        $type = ($_POST['start'] != '0') ? 1 : 0 ;
        //开始将数据写入到文件中
        _export_csv($data, $header_data, $path.$file_name, $type);
        //假设第100次 写入完毕,那么就可以进行下载文件啦。
        //可以先获取需要导出的总量,然后根据实际情况进行拆分数据,每次获取成功,进度条会显示进度。
        if ($_POST['start'] == 100) {
            die(json_encode(['code' => 'ok', 'file_path' => '/index.php?op=down&f='.$file_name]));
        } else {
            die(json_encode(['code' => 'no']));
        }
    }
    //简单的导出逻辑,可根据实际情况,进行开发。
    if (($_GET['op'] == 'down') && !empty($_GET['f'])) {
            _download_file($path.$_GET['f']);
            exit;
    }
?>

html页面部分:

<!DOCTYPE html>
<html lang="zh-CN">
    <head>
      <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="Demo">
        <meta name="keywords" content="">
        <!-- 新 Bootstrap 核心 CSS 文件 -->
        <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
        <!-- jQuery -->
        <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
        <!-- Title -->
        <title>Demo</title>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-lg-12" style="margin-top:20px">
                    <a id="export_file" href="javascript:void(0);" class="btn btn-primary btn-lg active" role="button">导出CSV文件</a>
                    <a id="download_file" style="display:none;" href="javascript:void(0);" class="btn btn-default btn-lg active" role="button">下载文件</a>
                </div>
            </div>
            <div id="process_one" class="row" style="display:none">
                <div class="col-lg-12" style="margin-top:20px">
                    <div class="progress">
                      <div id="progress-bar-one" class="progress-bar progress-bar-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
                      </div>
                    </div>
                </div>
            </div>
            <div id="process_two" class="row" style="display:none">
                <div class="col-lg-12" style="margin-top:20px">
                    <div class="progress">
                      <div id="progress-bar-two" class="progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
                        60%
                      </div>
                    </div>
                </div>
            </div>
        </div>
    <script>
        $(document).ready(function () {
            //导出CSV文件
            $("#export_file").click(function(){
                _get_data(0);
            });
            //获取数据的方法
            function _get_data(start)
            {
                $.ajax({
                                type: "POST",
                        url : 'index.php',
                        data: {
                            'start' : start
                        },
                        async: true,
                        dataType: "json",
                        beforeSend: function () {
                            $("#download_file").css('display', 'none');
                            $("#process_one").show();
                            $("#process_two").show();
                            },
                        success: function (result) {
                            if (result.code == 'ok') {
                                $("#download_file").show();
                                $("#download_file").attr("href", result.file_path);
                                $("#process_one").css('display', 'none');
                                $("#process_two").css('display', 'none');
                            } else {
                                start ++;
                                _get_data(start);
                                $("#progress-bar-one").css("width", start+'%');
                                $("#progress-bar-two").css("width", start+'%');
                                $("#progress-bar-two").html(start+'%');
                            }
                        },
                        error: function() {
                            alert("Server Error~");
                        }
                    });
            }
        });
    </script>
    </body>
</html>

阅完此文,您的感想如何?
  • 有用

    2

  • 没用

    0

  • 开心

    0

  • 愤怒

    0

  • 可怜

    0

1.如文章侵犯了您的版权,请发邮件通知本站,该文章将在24小时内删除;
2.本站标注原创的文章,转发时烦请注明来源;
3.交流群: PHP+JS聊天群

相关课文
  • mac开发接入微信公众号接口返回报错 cURL error 56: SSLRead() return error -9806

  • pecl安装程序时报错Array and string offset access syntax with curly braces is no longer supported

  • PHP的换行符是什么

  • 由于商家传入的H5交易参数有误,该笔交易暂时无法完成,请联系商家解决

我要说说
网上宾友点评
1 楼 IP 103.216.43.5 的嘉宾 说道 : 1589427560
能评论吗