PHP包含Include下载文件

ari*_*rik 1 php file download include

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

提前致谢!

web*_*ave 5

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;
}
Run Code Online (Sandbox Code Playgroud)

更改标题以满足您的特定需求.查看以上链接以获取更多信息.