【发布时间】:2017-11-23 07:02:14
【问题描述】:
所以我有这个 CI 项目,它可以从数据库(包含大量数据)转换为 CSV。
我尝试导出所有数据,点击“转换为 CSV”后,加载需要很长时间,浏览器会给我一个超时错误。
导出函数的代码如下:
ini_set('memory_limit', '-1');
set_time_limit(-1);
$prefKey = $this->session->flashdata('prefKey');
$searchKey = $this->session->flashdata('searchKey');
$withEmail = $this->session->flashdata('withEmail');
$list = $this->user_model->get_users($prefKey, $searchKey, $withEmail, "", "");
$headerArray = array("id", "prefecture_id", "industry_id", "offset", "name", "email");
// Header
$header = str_replace(",", "", $headerArray);
$datas = implode(',', $header) . "\r\n";
// Body
foreach($list as $body)
{
$orig_email = $body['email'];
$mstring = preg_replace("/^([^a-zA-Z0-9])*/",',',$orig_email);
preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $mstring, $matches);
$email = implode($matches[0]);
$datas .= $body["id"].",".$body["prefecture_id"].",".$body["industry_id"].",".$body["offset"].",".preg_replace('/[,]/',' ',$body["name"]).",".$email."\r\n";
}
$datas = mb_convert_encoding($datas, "SJIS-win", "UTF-8");
$csvFileName = "phpList_" . date('Ymd_His') . ".csv";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $csvFileName);
header('Content-Transfer-Encoding: binary');
while (ob_get_level() > 0)
{
ob_end_clean();
}
ob_start();
print trim($datas);
ob_flush();
ob_end_clean();
exit;
php.ini 的设置如下:
max_execution_time = 259200
max_input_time = 259200
memory_limit = 2G
还是一样的错误。
我无法确定它在哪里停止/超时。是从查询数据的时候开始的吗?将数据放在 CSV 中?还是正在下载?
根据上面提供的代码,我该如何做一个分批处理?我认为这只是一个一次性的过程。
这是模型:
public function get_users($prefecture_id, $industry_id, $filter, $limit, $start){
$this->db->select('*');
$this->db->from('company');
if ($filter) {
$this->db->like('email','@');
}
if (!empty($prefecture_id) && $prefecture_id != 99) {
$this->db->where('prefecture_id', $prefecture_id);
}
if (!empty($industry_id) && $industry_id != 99) {
$this->db->where('industry_id', $industry_id);
}
if (!empty($limit)) {
$this->db->limit($limit, $start);
}
$this->db->order_by('prefecture_id, industry_id, offset');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
【问题讨论】:
-
查看php.ini中的max_execution_time,也请开启error_reporting
-
@DavinderKumar 这些设置设置为大。仍然超时。
-
您的数据库中有多少用户?
-
@sintakonte 用户?你的意思是多少行?
-
是的 - 我的意思是,请也发布你的模型 - 因为我认为这是问题
标签: php codeigniter csv