From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Exporting data to a download file with SplTempFileObject

Exporting data to a download file with SplTempFileObject - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Exporting data to a download file with SplTempFileObject

- The SplTempFile object class creates a temporary file that you can write to and read. What's interesting about this class is that the temporary file is stored in memory. If it exceeds the allocated amount, the file is automatically moved to the systems temp directory and then deleted when the script comes to an end. One application where this could be useful, is for creating a download file. I've opened tempfile.php which you can find in the chapter two folder of the exercise files. At the top of the page is a multidimensional array of exchange rates. Then lower down the page, those exchange rates are displayed in a table. Of course, this sort of information would normally come from a database but I'm using an array to keep the example simple. Then at the bottom of the page, is a form with a submit button. We're going to use this to allow the user to download the exchange rates as a CSV file and we'll use the SplTempFile object class to create the file. The name of the submit button…

Contents