Created
July 6, 2011 07:16
-
-
Save mmacia/1066724 to your computer and use it in GitHub Desktop.
Generate files in-memory with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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