Is it possible to create a download form data to local machine without writing it to a file one the server?
I want to click export button on form and have "save as" file dialog popup, that would let me save the form data to my local machine with a view to upload it back to server at some later stage. I want to do this without saving the form data to a file on the server and then downloading it.
I have encoded form data into json format.
$json_data=json_encode($data);
I just can't find a way to download this without writing it to a file on the server first.
I just can't find a way to download this without writing it to a file on the server first.
It's actually kind of the opposite: if you look at any instructions on using PHP to send a download, such as Force file download with php using header() you will see that the actual content of the file needs to be read and echoed to the browser. Often this is done with the convenient function readfile, but this is just a more efficient version of echo file_get_contents($filename);
If you have some content in a string, and you echo it, the browser has no way of knowing whether you wrote it to a file on your server first; it sees the exact same output. The thing that it will look at to decide whether to display it or prompt to save is the headers, particularly the Content-Type
and Content-Disposition
headers.