Add to Favorites

PHP output streams - processing large amounts of data

Have you ever needed to process a large amount of data or use a function that explicitly writes to a file pointer such as fputcsv ?

You can go about creating a temporary file that you then need to remove later or you can let php handle this for you. There is a special stream you can open by the name of php://temp that will allow you open/create a file descriptor that you can write and read like it were a file. The php://temp stream will remain in memory until it reaches a threshold at which point it will swap over to using a temporary file on disk seamlessly. Using php://temp will save you time.

Example usage of php://temp :

$f = fopen('php://temp','r+');
fputcsv($f,array('one','two','three'));

The other option here would be to use a temporary file of your own making, but why re-invent the wheel and create extra steps like file cleanup ( which so many people forget to do, leaving their disk riddled with temporary files ).

There is one catch, in order to use php://temp you must have at least php version 5.1

Comments

Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up