您可以使該文件夾無法從網絡訪問(例如,將文件夾放置在htdocs外部或添加.htaccess規則)。
創建一個處理私人圖像的所有請求的PHP腳本。如果用戶被授權
-check如果用戶進行身份驗證
檢查以查看請求的圖像打開圖像並將其打印到瀏覽器
(需要設置:此腳本必須做到以下幾點正確的HTTP頭,以確保內容被視爲圖像)
演示
getimage.php
if (LoggedInUserCanAccessThisFile())//this is optional user define function as requirement if you want that only login user can see image then with the help of your session variables or cookies you can return this function true or false
{
$file = 'privatedir/image.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
exit();
}
home.php/otherpage.php
<img src="getimage.php" />
(你可以使用SRC = 「getimage.php?用戶id = 123」,進入getimage.php並檢查該用戶登錄或不顯示圖像)
(也可以使用SRC = 「getimage.php?用戶ID = 123 & imgfilename = image3.jpg」 動態圖像的代碼,並進入getimage.php作爲
$file = 'privatedir/'.$_GET["imgfilename"];
)
參考:http://www.naturefocused.com/articles/image-protection.html –