Read Large XLSX Files in PHP with Streaming
Read large XLSX files in PHP with bounded-memory streaming, chunk callbacks, column projection, progress reporting and resumable processing.
Updated
Full packageIndividual XLSXStreamingLow memory
MnbExcel::largeRead()LargeXlsxReadSession::chunk()onlyColumns()transform()memoryGuardRatio()What you will learn
- Configure a large-read session explicitly.
- Process batches without retaining previous rows.
- Apply source projection and transformations before callbacks.
Configure the large reader#
$session = MnbExcel::largeRead('orders.xlsx')
->sheet('Orders')
->withHeader()
->onlyColumns(['Order ID', 'Customer', 'Amount'])
->preserveNumericStrings()
->convertDates(true, 'Y-m-d')
->memoryGuardRatio(0.80)
->timeBudgetSeconds(900)
->progress(function (array $state): void {
printf("Rows: %d\n", $state['rows_read'] ?? 0);
});Process one batch at a time#
$result = $session->chunk(1000, function (array $rows, array $state): void {
foreach ($rows as $row) {
// Validate or transform.
}
// Insert this batch and release it before the next callback.
});Add reusable transformations#
$session
->transform(function (array $row): array {
$row['Amount'] = (float) $row['Amount'];
$row['Customer'] = trim((string) $row['Customer']);
return $row;
})
->chunk(1000, $callback);Use native extensions for the strongest guarantee#
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue