我在一個upload檔案夾中有 3 個子檔案夾。我的代碼如下所示:
if(isset($_SESSION["u_type"]) && $_SESSION["u_type"] == 3) {
$files = scandir($path."/3/") //$path is set somewhere above
//...
}
它作業正常,但您實際上可以添加一個簡單的 html 標簽,例如<img src="uploads/2/somathing.png/> ,即使您的“用戶型別”設定為 3,您也可以從子目錄 2 獲取任何檔案。
有什么辦法可以預防嗎?
我已經嘗試過:
我在其中使用了 .htacces Options- Indexes,但它只會直接列出檔案。
uj5u.com熱心網友回復:
是的,有一種方法可以做到這一點,它需要 Apache 網路服務器(用于 .htaccess 檔案):
拒絕上傳檔案夾中的所有內容
將此添加到您的上傳檔案夾中.htaccess:
order deny,allow
deny from all
創建代理php腳本 image.php
創建一個名為的腳本image.php并在其中檢查您的會話:(您可能需要根據您的要求更新此腳本,這是一個僅支持 jpeg 的簡單示例)。
<?php
session_start();
//check session for permission here!
$userTypeId = 1; //change this to requirements
header('Content-type: image/jpeg');
echo file_get_contents("uploads/" . $userTypeId . "/" . $_GET['image']);
現在通過訪問代理腳本訪問影像
<img src="image.php?image=yourimage.jpg" />
更新; 你可以選擇重寫image.php?image=x.jpg
你可以選擇重寫這個路徑,.htaccess這樣看起來好像沒有代理.php腳本在作業。
RewriteRule ^/a_chosen_path_name/([^\.] )\.(png|jpg|gif)$ /image.php?image=$1.$2 [NC,L]
之后您可以使用:
<img src="a_chosen_path_name/yourimage.jpg" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/349841.html
