I'm using this bit of code to download a file (path_facture_name
) from the server to the client browser :
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($path_facture_name) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($path_facture_name));
ob_clean();
flush();
readfile($path_facture_name);
ob_end_flush();
# ----
# Some other PHP code
# ----
This works just fine, but when the file is downloaded, the script is ended, and the part Some other PHP code
will never be executed.
So, my question is, is there a better way to download a file from the server that don't abort the execution of the next part of the code ?
I've tried to use <iframe>
or JavaScript
code to redirect the window
to a sipparate .php
file that will handle the download. But that didn't work because this feature that I wanna add is a part of an 18 years old complex php
CRM that I can't easily/freely edit.
I'm looking for a PHP
solution or guidelines.
The answer for this question is @ADyson's comment above :
« "and reload the current php page"...you can't do that, only the browser can do that in this situation. You've already given your response to the request in the form of a file download. The only way to "reload the current page" from the server would be to send some new HTML for the browser to display. But you can't respond to a single HTTP request with both a file download and a HTML document. It's physically impossible. You've already set headers indicating to the browser that the response is a file for download. »
So this is a HTTP
limitation. We can't do a multi-part response.