Streaming Large CSV Database Exports in Laravel Without Memory Exhaustion
Why Spreadsheet Libraries Crash on Multi-Million Row Archives
Popular spreadsheet packages build an in-memory cell map before serializing a file to disk. While that works nicely for formatted Excel reports of 5,000 rows, exporting 2 million historical campaign logs for compliance archiving quickly consumes several gigabytes of RAM and triggers PHP fatal memory errors.
By combining native PHP fopen() streams, fputcsv(), and Laravel's chunkById() on base query builder rows (toBase()), you can archive millions of records to S3 or Dropbox while keeping worker memory consumption below 20 MB.
Streaming Rows to a Temporary File Handle and Cloud Disk
Write rows directly to a temporary local stream using tmpfile(), rewind the pointer, and hand the stream resource directly to Storage::writeStream() so Flysystem uploads the file in chunks without loading the entire CSV string into PHP memory:
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
$stream = tmpfile();
fputcsv($stream, ['ID', 'Recipient', 'Status', 'Sent At']);
DB::table('campaign_logs')
->select(['id', 'recipient', 'status', 'sent_at'])
->where('campaign_id', $campaignId)
->orderBy('id')
->chunkById(2000, function ($rows) use ($stream) {
foreach ($rows as $row) {
fputcsv($stream, [
$row->id,
$row->recipient,
$row->status,
$row->sent_at,
]);
}
});
rewind($stream);
Storage::disk('archives')->writeStream(
"campaigns/campaign-{$campaignId}.csv",
$stream
);
if (is_resource($stream)) {
fclose($stream);
}
Direct Browser Downloads with StreamedResponse
When an admin triggers an on-demand CSV download from the browser, return response()->streamDownload() writing to php://output. The browser begins receiving bytes immediately, preventing Nginx or Cloudflare 504 gateway timeouts while the query streams:
return response()->streamDownload(function () use ($campaignId) {
$output = fopen('php://output', 'w');
fputcsv($output, ['ID', 'Recipient', 'Status', 'Sent At']);
DB::table('campaign_logs')
->select(['id', 'recipient', 'status', 'sent_at'])
->where('campaign_id', $campaignId)
->orderBy('id')
->chunkById(2000, function ($rows) use ($output) {
foreach ($rows as $row) {
fputcsv($output, [(string) $row->id, $row->recipient, $row->status, $row->sent_at]);
}
flush();
});
fclose($output);
}, "campaign-{$campaignId}.csv", [
'Content-Type' => 'text/csv; charset=UTF-8',
]);
Written by Gulfaraz Arshad
Software Developer at Cruise Brains specializing in Laravel, PHP, MySQL performance optimization, and high-throughput API architectures.