Skip to content

Instantly share code, notes, and snippets.

@mmacia
Created July 6, 2011 07:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmacia/1066724 to your computer and use it in GitHub Desktop.
Save mmacia/1066724 to your computer and use it in GitHub Desktop.
Generate files in-memory with PHP
<?php
/**
* This snippet shows a PHP neat trick: build a file into memory without use the filesystem.
*/
$fp = fopen('php://memory', 'rw'); // open an in-memory handler
for ($i = 0; $i < 100; ++$i) { // write some data to handler
fwrite($fp, $i."\n");
}
fseek($fp, 0); // rewind file pointer to the begining
$content = '';
while (!feof($fp)) { // read agian the file handler and put data in a variable
$content .= fread($fp, 8192);
}
fclose($fp); // free allocated memory
echo $content;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment