Prompt to save a PHP dynamically created file to local computer
I am looking for a way that the results from a search generates a file
that can be downloaded and saves by the user into their computer. I am
already able to generate the file, however, it gets stored on the server
automatically rather than prompting the user where to store it.
In the search form the user can select a checkbox to generate a CSV file
of the output (I would prefer to just have a download button on the
results page but was unable to figure out how to do that):
https://www.evernote.com/shard/s68/sh/54e95567-d91e-4649-a5c1-35b5f8929c13/a4bc4fed2a2c7f7801099f6d5711c49e
Then in the next page the results are shown on the page and the
appropriate file is also generated.
Code that generates the file on the results page:
if($print_flag == 1) {
$filename = "exportfile" . date("Y-m-d_H_m_s") . ".csv";
$handle = fopen($filename, 'w+');
fputcsv($handle, array($column1,$column2));
fclose($handle);
}
$print_flag is set if the user checks the CSV checkbox in the prior form.
This creates a file that is stored on the server rather than allowing the
user to save it locally.
I saw this previous questions PHP save file to users computer but could
not figure out how to add those headers or whether that was the right
approach.
Thanks!
No comments:
Post a Comment