【问题标题】:PHP Downloading File(s) outside webrootPHP 在 webroot 之外下载文件
【发布时间】:2012-02-27 01:27:30
【问题描述】:

我在 webroot 之外保存了许多文档。

我想点击一个链接,打开一个新窗口 (target="_blank"),然后强制下载找到的文件。

这是我到目前为止所得到的,但我的结果在浏览器弹出窗口中显示 gobble-de-gook,而不是强制下载到桌面:

function download($filelocation){

  $filename = basename($filelocation); 
  if (file_exists($filelocation)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.$filename); 
    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($filelocation));
    ob_clean();
    flush();
    readfile($filelocation);
    exit;
  }

}

在新的浏览器窗口中,我只需使用文件的特定路径调用 download() 函数。

它肯定会找到文件,但现在我只是想知道我缺少什么 header() 来强制文件通过浏览器。

【问题讨论】:

标签: php header download


【解决方案1】:

缺少这个:

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

【讨论】:

  • 试过了,得到了同样的结果。我什至更改了内容类型以反映实际的文件类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
  • 1970-01-01
相关资源
最近更新 更多