2012-06-25 71 views
0

我使用馬丁·巴克的代碼/回答(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); 
+2

你是如何訪問它? – Blaster

+0

我firefox找不到download.php,你是否改寫你的網址? – jeroen

+0

嗨Jeroen - 不,不重寫網址 –

回答

0

確保用戶的php腳本以對該目錄的讀取權限運行。

在大多數Debian衍生產品上嵌入apache的php,用戶將是'www-data'。

+0

它是一個共享主機環境與CP訪問(Hetzner在南非),所以不太確定,如果我能夠改變用戶,儘管CHMOD顯然是可能的。 –

+0

你應該問你的共享主機,如果有可能你有一個監獄殼或類似的shell訪問。此外,在共享主機環境中,由於suexec和'suPHP'之類的原因,您經常將自己的用戶作爲自己的用戶運行,因此如果情況如此,您只需要chmod。 – AndrewPK

0

我最近有同樣的問題,其中readfile()和fpassthru()在我的服務器上不起作用。

我最終做的是根據需要爲這些文件創建符號鏈接並將其傳遞給用戶。你可以學習如何創建符號鏈接here

我用

exec("ln -s source_file_full_path full_path_to_fake_file");

,如果你想你的用戶能有像「http://somesite.com/folder/fake_file.pdf」的鏈接,然後完整路徑是到「文件夾」住你的服務器上,你會包括「fake_file。 pdf'在你的假文件路徑中。

然後過期鏈接我又打了一個電話找到所有符號鏈接的創建日期超過x分鐘。你可以在this answer看到如何做到這一點。 (這可能是一個cron的工作,以確保他們按時到期。)