doupuxuan5784 2011-07-06 00:24
浏览 14
已采纳

PHP:发布下载文件的链接

I found this code that allows me to opens a window and allows the user to download a file (download.php)

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;

It works well to allow the user to download a file from the server via a pop up window, but I have two issues:

  1. Before executing this block of code, I have some text printing on the screen (browser). However, when the .php file is executed with this block at the very end, nothing prints on the screen, and it goes directly to the download. How do I ensure that things are printed on the browser and then allow the file to be downloaded?

  2. Then, I thought maybe I'll instead insert a link after things are printed, but I'm having trouble getting this to work. When I click on the link to download (href='download.php'), a blank screen is returned and nothing else.

Could someone chime in and help? TIA!

  • 写回答

3条回答 默认 最新

  • du90093662774150 2011-07-06 00:27
    关注

    You can't return HTML for the browser and a file download from a single page request from the browser. It expects one or the other. You could open a popup window with text, which then does a Javascript redirect to a second PHP script which sends the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 VB.NET画图时的撤销编程
  • ¥15 C# webservice 参数特殊字符处理
  • ¥15 关于控制时间更新的一个操作
  • ¥15 JAVA中使用poi创建word文件,实现页面边框环绕页眉
  • ¥15 matlibplot绘图图形切线问题
  • ¥15 flink 深度优先搜索
  • ¥15 python for everybody 使用Beautifulsoup构建soup对象的时候疯狂报错
  • ¥15 mac playcover上原神在登录后的加载页面闪退,重新下载原神IPA和playcover均无效
  • ¥20 node-dev能不能离线安装?
  • ¥20 六子棋博弈基于Alpha-Beta剪枝搜索算法的优化