【问题标题】:Download file .ZIP with PHP automatically使用 PHP 自动下载文件 .ZIP
【发布时间】:2021-09-02 18:13:46
【问题描述】:

我想从我的服务器上的一个文件夹中下载一个文件,然后自动开始下载,而不会看到原始文件链接。

这是我当前的脚本,但它下载了一个无效的损坏文件。

$filename = "dsk.zip";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename="./zz/'.$filename.'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize("./zz/".$filename));
ob_end_flush();
@readfile("./zz/".$filename);

你能帮帮我吗?文件 dsk.zip 位于文件夹 zz 中。

【问题讨论】:

  • 这里有语法错误,比如filename="./zz/'.$filename.'"。先解决这些问题。
  • filename="./zz/'.$filename.'" 您不能在用户 PC 上定义文件夹,实际上文件名只是一个提示,用户可以在他们将看到的对话框中忽略。所以使用一个简单的filename=".$filename.'"
  • 这能回答你的问题吗? Create a zip file and download it
  • 最好的办法是改变你的server文件夹名或文件名,但是比较麻烦。 somedomain.com/FOLDER_NAME_/dsk.zip somedomain.com/download/dsk-20210902-15464.zip
  • 这段代码会发生什么?如果您移除错误抑制,您会收到错误消息吗?

标签: php


【解决方案1】:
$file = "dsk.zip"; // this is what you will get from a param (i.e. ?file=dsk.zip) or from DB query

header("Cache-Control: public");

header("Content-Description: File Transfer");

header("Content-Disposition: attachment; filename='zz/'.$file"); 

header("Content-Length: ".filesize($file));

header("Content-Type: application/force-download");

header("Content-Transfer-Encoding: binary");

readfile('zz/'.$file);

【讨论】:

  • 你建议他们在这里改变什么?
  • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
相关资源
最近更新 更多