How to output as a file without saving it to the server

I serve some records from a MySQL database using PHP fputcsv() , creating a file on the server, filling it in, and then linking to it on the next page.

This works fine, but since it can be sensitive data, I don’t want the files to hang on the servers when they were created for (possibly) a one-time download.

So what I want to know is: is there a way to create this file and upload it for download without actually writing a permanent file on the server?

For example, is it possible to create a comma-separated string instead of using fputcsv() and use it with the correct headers in the output buffer?

The obvious point is to delete the file, but I need to wait until the client first downloads it, so it will be difficult for him to decide when to do it.

Any suggestions are welcome.

The code:

 $fp = fopen($filename, 'w'); fputcsv($fp, array("Last Name", "First Name")); foreach ($result as $fields) { fputcsv($fp, $fields); } fclose($fp); 

http://php.net/manual/en/function.fputcsv.php

+4
source share
2 answers

Instead, why not just output the page from the cms mime type and then output the file to the user?

It works in charm, the file is never created and transmitted to the client as one.

Something like that:

 header("Content-type: application/csv"); header("Content-Disposition: attachment; filename=file.csv"); header("Pragma: no-cache"); header("Expires: 0"); echo "col1,col2"; for($i=0; $i<25;$i++) { echo "key :".$i.", ".($i*$i)."\r\n"; } 

You should be able to check it as is and see how it works.

Added beauty lies in the fact that most users will be directed to downloading the file, rather than opening, so the user does not even leave the page (most of the time).

+3
source

fputcsv () is an incredible little function, so I won't give it up.

Instead, I suggest you play with the built-in PHP I / O Wrappers

You can do this, for example, to "stream" your CSV data line by line (given the different output buffers, but that's a different story):

 <?php header('Content-type: text/csv; charset=UTF-8'); header('Content-disposition: attachment; filename=report.csv'); $fp = fopen('php://output','w'); foreach($arrays as $array) fputcsv($fp, $array); 

This works fine, but if something goes wrong, your users will have a broken download.

So, if you have too much data, you can just write to the stream in memory, just change php://output to php://memory and move something around:

 <?php $fp = fopen('php://memory','rw'); // our generateData() function might throw an exception, in which case // we want to fail gracefully, not send the user a broken/incomplete csv. try { while($row = generateData()) fputcsv($fp, $row); }catch(\Exception $e){ // display a nice page to your user and exit/return } // SUCCESS! - so now we have CSV data in memory. Almost like we'd spooled it to a file // on disk, but we didn't touch the disk. //rewind our file handle rewind($fp); //send output header('Content-type: text/csv; charset=UTF-8'); header('Content-disposition: attachment; filename=report.csv'); stream_get_contents($fp); 
+4
source

All Articles