【问题标题】:PHP Download File with Include包含包含的 PHP 下载文件
【发布时间】:2011-02-13 10:22:18
【问题描述】:

我如何使用 PHP 的 include 函数来包含一个文件,然后修改标题以便它强制 - 至少它是这样被调用的 - 浏览器下载 ITSELF(PHP 文件)。是否也可以修改预设的保存名称,以便将扩展名从 *.php 更改为其他名称?

提前致谢!

【问题讨论】:

  • 您指的是哪个 PHP 文件?包含文件还是包含的文件?
  • 包括。这就是为什么我包含一个文件。所以不知道包含文件的路径,只下载包含的文件。

标签: php file download include


【解决方案1】:

PHP include 函数将解析文件。您要做的是使用file_get_contentsreadfile

这是 readfile 文档中的一个示例:

$file = 'somefile.gif';

if (file_exists($file)) {
    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;
}

更改标题以满足您的特定需求。查看以上链接了解更多信息。

【讨论】:

  • 好的。谢谢你!但是,无论如何,我怎样才能下载文件呢?甚至强制下载?
  • with: echo file_get_contents('source_of_file');
  • 如何将 file_get_contents 与子文件夹一起使用?它只是行不通:(
  • 尝试使用绝对路径。为了获得更大的灵活性,您可以在 $_SERVER['DOCUMENT_ROOT'] 前面添加一个相对路径。
猜你喜欢
  • 2012-02-13
  • 2011-08-02
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-09
相关资源
最近更新 更多