【发布时间】:2016-04-09 10:05:36
【问题描述】:
我希望能够使用 php 从我的服务器下载文件。到目前为止它工作得很好,但仅适用于其中包含文本的文件(.txt.php,因此包含简单文本的文件(即使我有一个有趣的现象,在文本开始之前总是有一个空行......想法为什么?),但是当我尝试下载.jpg 文件或.exe 时,它根本不起作用(尝试打开时出错...)
这是我使用的代码:
<?php
session_start();
$file = basename($_GET['file']);
$path = 'uploads/'.$_SESSION['userid']."/".$file;
?>
<?php
if(!file_exists($path)){
die("file not found");
} else {
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($path);
exit;
}
【问题讨论】:
-
如何调用这个脚本?我认为您的服务器通过 mime 类型为 php 和 txt 文件设置了标头。
-
这是另一个 php 文件中的链接,如下所示:
echo "<a href='download.php?file=".$entry."'>".$entry."</a>\n".'<br/>';