實際工作中,我們經常導出報表的需求,當導出數據量過大的時候,經常會遇到超時和内存溢出的問題。
解決方案一
超時可用: 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>