我几乎逐字逐句地使用 Martin Barker 的代码/答案(PHP 来保护 PDF 和 DOC),唯一的区别是我要保护的文件位于 public_html 文件夹上方的用户文件夹中
文件夹结构
/users/websupport
/public_html
要下载的文件位于:
/users/websupport/FileToDownload.pdf
download.php 文件位于
/public_html/download.php
但 Firefox 告诉我它无法在 Firefox 中找到该文件在 download.php 中找不到该文件。
我已经通过 ftp 验证了该文件是否存在。
如果将文件放在 webroot 之外,我是否需要在站点 .htaccess 中添加一些内容?只是不确定我哪里出错了。以下是 download.php 中的代码
//check users is loged in and valid for download if not redirect them out
// YOU NEED TO ADD CODE HERE FOR THAT CHECK
// array of support file types for download script and there mimetype
$mimeTypes = array(
'doc' => 'application/msword',
'pdf' => 'application/pdf',
);
// set the file here (best of using a $_GET[])
$file = "../users/websupport/2011cv.pdf";
// gets the extension of the file to be loaded for searching array above
$ext = explode('.', $file);
$ext = end($ext);
// gets the file name to send to the browser to force download of file
$fileName = explode("/", $file);
$fileName = end($fileName);
// opens the file for reading and sends headers to browser
$fp = fopen($file,"r") ;
header("Content-Type: ".$mimeTypes[$ext]);
header('Content-Disposition: attachment; filename="'.$fileName.'"');
// reads file and send the raw code to browser
while (! feof($fp)) {
$buff = fread($fp,4096);
echo $buff;
}
// closes file after whe have finished reading it
fclose($fp);