處理大型 CSV 檔案(數百 MB 甚至 GB 級別)時,傳統 fopen + fgetcsv 逐行讀取容易導致超時或記憶體耗盡。本文介紹基於 SplFileObject 的高效解決方案,並封裝為可複用的 CsvReader 類。
方案一:file() 載入全檔 → 記憶體爆炸 ❌
方案二:fgets() 逐行計數 → 速度慢、易超時 ❌
方案三(推薦):SplFileObject::seek() 定位檔案末尾 + key() 獲取行號 ✅
$csv_file = 'path/bigfile.csv';
$spl_object = new SplFileObject($csv_file, 'rb');
$spl_object->seek(filesize($csv_file));
echo $spl_object->key(); // 即時獲取總行數
$csv_file = 'path/bigfile.csv';
$start = 100000; // 從第 100,000 行開始
$num = 100; // 讀取 100 行
$spl_object = new SplFileObject($csv_file, 'rb');
$spl_object->seek($start);
$data = array();
while ($num-- && !$spl_object->eof()) {
$data[] = $spl_object->fgetcsv();
$spl_object->next();
}
print_r($data);
<?php
class CsvReader {
private $csv_file;
private $spl_object = null;
private $error;
public function __construct($csv_file = '') {
if ($csv_file && file_exists($csv_file)) {
$this->csv_file = $csv_file;
}
}
public function set_csv_file($csv_file) {
if (!$csv_file || !file_exists($csv_file)) {
$this->error = 'File invalid';
return false;
}
$this->csv_file = $csv_file;
$this->spl_object = null;
}
public function get_csv_file() {
return $this->csv_file;
}
private function _file_valid($file = '') {
$file = $file ? $file : $this->csv_file;
if (!$file || !file_exists($file)) return false;
if (!is_readable($file)) return false;
return true;
}
private function _open_file() {
if (!$this->_file_valid()) {
$this->error = 'File invalid';
return false;
}
if ($this->spl_object == null) {
$this->spl_object = new SplFileObject($this->csv_file, 'rb');
}
return true;
}
/**
* 獲取指定區間資料
* @param int $length 讀取行數(預設讀到檔案結尾)
* @param int $start 起始行號(從 1 開始)
* @return array|false
*/
public function get_data($length = 0, $start = 0) {
if (!$this->_open_file()) return false;
$length = $length ? $length : $this->get_lines();
$start = $start - 1;
$start = ($start < 0) ? 0 : $start;
$data = array();
$this->spl_object->seek($start);
while ($length-- && !$this->spl_object->eof()) {
$data[] = $this->spl_object->fgetcsv();
$this->spl_object->next();
}
return $data;
}
/**
* 獲取檔案總行數
* @return int|false
*/
public function get_lines() {
if (!$this->_open_file()) return false;
$this->spl_object->seek(filesize($this->csv_file));
return $this->spl_object->key();
}
public function get_error() {
return $this->error;
}
}
<?php
include('CsvReader.class.php');
$csv_file = 'path/bigfile.csv';
$csvreader = new CsvReader($csv_file);
// 獲取總行數
$line_number = $csvreader->get_lines();
// 讀取前 10 行
$data = $csvreader->get_data(10);
echo $line_number, "\n";
print_r($data);
此類不僅限於 CSV,只要將 fgetcsv() 改為 current() 即可處理任意大型文字檔案(如日誌、JSON Lines 等)。
seek() 可直接跳轉到任意行,無需循序讀取SplFileObject 為 C 底層實現,比純 PHP 循環快數倍