dongxinche1264 2018-11-02 12:02
浏览 192
已采纳

PHP 5.3.3:从服务器下载文件并继续执行脚本

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.

  • 写回答

1条回答 默认 最新

  • doushou6480 2019-01-09 10:39
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?