我使用馬丁·巴克的代碼/回答(PHP to protect PDF and DOC)幾乎verbatum,唯一不同的是我保護的文件在上面的public_html文件夾我的用戶文件夾PHP保護的文件訪問
文件夾結構
/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);
你是如何訪問它? – Blaster
我firefox找不到download.php,你是否改寫你的網址? – jeroen
嗨Jeroen - 不,不重寫網址 –