php://memory And Maxmemory¶
<?php
// build something entirely in memory...
$fp = fopen('php://memory', 'r+');
fputcsv($fp, ['name', 'email', 'role']);
fputcsv($fp, ['Alex', 'alex@example.com', 'CTO']);
fputcsv($fp, ['Jane', 'jane@example.com', 'Dev']);
rewind($fp);
$csv = stream_get_contents($fp);
fclose($fp);
// send as download
header('Content-Type: text/csv');
echo $csv;
// php://temp, same but spills to disk over 2MB... or more!
$fp = fopen('php://temp/maxmemory:8388608', 'r+');
// 8MB in RAM, then auto-swaps to a temp file
// no gc, no unlink(), no /tmp pollution
// the stream disappears when $fp is closed
php://memory is a stream that lives in RAM.
php://temp starts in RAM, spills to disk after 2MB (configurable, see below!).
No tmpfile(). No temp directory. No cleanup.
Perfect for building files in memory before sending in PHP!
Did you know the maxmemory trick?
See Also¶
PHP Features¶
Last updated: 14 July 2026