Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a temporary file

Tags:

I'm trying to create a short PHP script that takes a JSON string, converts it to CSV format (using fputcsv), and makes that CSV available as a downloaded .csv file. My thought was to use tmpfile() to not worry about cronjobs or running out of disk space, but I can't seem to make the magic happen.

Here's my attempt, which is a converted example from the PHP docs of readfile:

$tmp = tmpfile(); $jsonArray = json_decode( $_POST['json'] ); fputcsv($tmp, $jsonArray);  header('Content-Description: File Transfer'); header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename='.basename($tmp)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($tmp));  ob_clean(); flush(); readfile($tmp); 

I feel like the Content-Disposition and Content-Transfer-Encoding headers are wrong, but I'm not sure how to fix them. For example, basename($tmp) doesn't seem to return anything, and I'm not sure text/csv transfers as binary encoding. Likewise, echo filesize($tmp) is blank as well. Is there a way to do what I'm attempting here?

[Edit]

**Below is the working code I wrote as a result of the accepted answer:*

$jsonArray = json_decode( $_POST['json'], true ); $tmpName = tempnam(sys_get_temp_dir(), 'data'); $file = fopen($tmpName, 'w');  fputcsv($file, $jsonArray); fclose($file);  header('Content-Description: File Transfer'); header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename=data.csv'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($tmpName));  ob_clean(); flush(); readfile($tmpName);  unlink($tmpName); 
like image 205
Impirator Avatar asked Mar 13 '13 16:03

Impirator


People also ask

Where can I find temp download files?

Temporary Files Location The Temporary Files in Windows are typically found located in two locations: %systemdrive%\Windows\Temp. %userprofile%\AppData\Local\Temp.

Where can I find temporary files in Windows 10?

From the Start menu, open the Run dialog box or you can Press the "Window + R" key to open the RUN window. Type "%temp%" and click on the OK button. Note: And, you can also find the temporary files in the "Temp" folder by typing the "temp" command or by typing the "C:\Windows\Temp" path in the Run window.


1 Answers

Note that tmpfile() returns a file handle, but all the functions you have need a file path (i.e a string), not a handle - notably basename, filesize, and readfile. So none of those function calls will work correctly. Also basename won't return a file extension either. Just call it whatever you want, i.e

'Content-Disposition: attachment; filename=data.csv' 

As @Joshua Burns also says, make sure you're passing in an array to fputcsv or use the assoc parameter.

like image 114
Ivo Avatar answered Nov 09 '22 10:11

Ivo