php 大數據量導出

之前的正常導出,幾萬條數據就把內存擠爆了,優化了一下導出方式,內存無壓力導出速度槓槓的

	//會員時手機導出
	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);
		}
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章