【发布时间】:2016-05-11 09:59:24
【问题描述】:
上传后我正在从 Ftp 文件夹下载文件。
问题是当我打开 txt 文件时,它会显示附加文件内容的 html 页面源
如果我预览图像文件(jpg 或 jpeg),则显示图像已损坏
如果我打开 pdf 错误:无法加载 Pdf 文档
请告诉我我哪里错了。
这是我的代码:
if (isset($_GET['id']) && basename($_GET['id']) == $_GET['id']) {
$filename = $_GET['id'];
} else {
$filename =NULL ;
}
$err = 'Sorry, the file you are requesting is unavailable.';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo $err;
} else {
// define the path to your download folder plus assign the file name
$path = '/home/devestctrl/public_html/wp-content/uploads/'.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
// get the file size and send the http headers
$size = filesize($path);
header('Content-Type: application/octet-stream');
header('Content-Type: '.$mime);
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error messages if the file can´t be opened
$file = @ fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit();
} else {
echo $err;
}
} else {
echo $err;
}
}
我使用$filename 的回声检查;
它在文件中显示输出,而不是在页面中打印。
错误显示:
抱歉,您请求的文件不可用。
出错后我也可以下载,但在文件中显示$filename。
插入表格:
echo "<tr><a href='?id=" . $row["FileupName"]. "'>".$row["FileupName"]."</td></tr>";
【问题讨论】: