簡體   English   中英

PHP:僅在已登錄用戶的情況下才能訪問根文件夾(而不是public_html)中的上載文件?

[英]PHP: make uploaded file in root folder (not in public_html) accessible only for logged in user?

我制作了一個上傳表單,並將文件存儲在(根)“ ../ upload_file”文件夾中

問題是授權(登錄)用戶只能如何下載該上載的文件? 因為瀏覽器無法處理根網址,例如:www.web.com/../upload_file/test.pdf

請我新手

感謝:D


好吧,我想澄清一下。 實際上,我的問題類似於: 如何保護文件免遭未經授權的下載

未解決的地方

您需要創建一個將驗證用戶身份的php腳本,然后在php腳本中輸出上載文件的內容。 在設置輸出文件的標題之前,請確保沒有回顯任何內容。 如何將文件內容加載到變量中應該有代碼。

$fileContents = file_get_contents("test.pdf");

header("Cache-Control: public, must-revalidate");
header("content-disposition: attachment; filename=test.pdf");
header("content-type: application/pdf");
header("content-length: " . strlen($fileContents));

//output file contents
echo $fileContents;

用戶永遠不能直接訪問上載文件夾。

訪問控制取決於您的服務器操作系統和服務器,例如Windows Server(IIS)或Linux(Apache,Ngix)...

防止從URL訪問文件的最佳方法是您的Webroot 外部設置上傳路徑。文件只能由您的服務器訪問。

更新:如何按用戶訪問文件

創建一個名為readpdf.php的php文件。

header("Content-type:application/pdf");
//if you just want to show the file on broswer, delete the line below
header("Content-Disposition:attachment;filename='downloaded.pdf'");
readfile("progit.pdf");

通過訪問http://localhost/readpdf.php讀取文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM