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.

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

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛