之前的正常导出,几万条数据就把内存挤爆了,优化了一下导出方式,内存无压力导出速度杠杠的

	//会员时手机导出
	public function user_outputexcel(){
		$wheres=$_COOKIE['lists_where'];
		if($wheres){
			$data=$this->db->Getlist("SELECT mobile,score,source,time,money,username FROM `@#_member` where mobilecode='1' and auto_user !=1 ".stripslashes($wheres));
		}else{
			$data=$this->db->Getlist("SELECT mobile,score,source,time,money,username FROM `@#_member` where mobilecode='1' and auto_user !=1");
		}

		$count = count($data);

		$num = 0;
		$f = null;
		foreach ($this->getCounts($count, function ($fp) use (&$f) {
			$f = $fp;
		}) as $value) {
			$num++;
			if (1000 === $num) {
				ob_flush();
				flush();
				$num = 0;
			}
			foreach ($value as $v) {
				$put = json_decode(json_encode($v), true);
				fputcsv($f, $put);
			}

			fclose($f);
		}
//导出合并后的文件
		$this->mergeFile(function ($file) {
			error_reporting(0);
			header('Content-Encoding: UTF-8');
			header("Content-Type: text/csv; charset=UTF-8");
			header("Cache-Control: max-age=0");
			header("Content-Description: File Transfer");
			header('Content-disposition: attachment; filename=userlist.csv'); // 文件名
			header("Content-Type: application/csv");
			header("Content-Transfer-Encoding: binary");
			header('Content-Length: ' . filesize($file));
			readfile($file);//输出文件;
			self::clearFile();
		});
	}
	/**
	 * 获取阶段导出数据
	 */
	private function getCounts($count, $handle)
	{
		$pageCount = ceil($count / 1000);
		for ($i = 0; $i < $pageCount; $i++) {
			$file = __DIR__ . '/temp/_' . $i . '.csv';

			touch($file);
			$fp = fopen($file, 'w'); //生成临时文件
			if ($handle instanceof \closure) {
				$handle($fp);
			}
			yield $this->db->Getlist("SELECT mobile,username,
								case when score >0 then 'Yes' else 'No' end as scores,
								case when score >0||money>0 then 'Yes' else 'Yes' end as moneys,
								source,from_unixtime(time, '%Y-%m-%d %H:%i:%S')as time  FROM `@#_member` where mobilecode='1' and auto_user !=1
          						limit 1000  offset ".($i*1000)."  ");

		}

	}

	/**
	 * 处理数组格式
	 */
	private static function makeArray(array $data)
	{
		$new = [];
		foreach ($data as $key => $value) {
			$new[] = array_values($value);
		}

		return $new;
	}

	/**
	 * 合并文件
	 */
	private function mergeFile($handle)
	{
		$fileList = glob(__DIR__ . '/temp/*.csv');
		$count = count($fileList);
		for ($i = 0; $i < $count; $i++) {
			if ($i > 0) {
				file_put_contents($fileList[0], file_get_contents($fileList[$i]), FILE_APPEND);
			}
		}
		if ($handle instanceof \closure) {
			$handle($fileList[0]);
		}
	}

	/**
	 * 清除文件
	 */
	private static function clearFile()
	{
		error_reporting(0);
		$fileList = glob(__DIR__ . '/temp/*.csv');
		foreach ($fileList as $value) {
			unlink($value);
		}
	}
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐