1
我正在創建會員的管理數據庫。我想將所有成員圖片放在網站根目錄之上,以便他們無法通過網址訪問,但我想在會員資料中顯示該成員的圖片。我正在使用PHP。我怎樣才能做到這一點?!謝謝!會員圖片以上網站根目錄
我正在創建會員的管理數據庫。我想將所有成員圖片放在網站根目錄之上,以便他們無法通過網址訪問,但我想在會員資料中顯示該成員的圖片。我正在使用PHP。我怎樣才能做到這一點?!謝謝!會員圖片以上網站根目錄
您可以創建一個基於某些輸入參數來讀取文件的php文件,並同時決定是否允許訪問者查看該文件。
像這樣(簡化的示例):
<?php
// presuming this file is in the root of your site
// define some directory where the actual images are
$dir = realpath(dirname(__FILE__) . '/../profile-images');
// presuming this file is called with something like
// image.php?profileImage=fireeyedboy.jpg
if(isset($_GET[ 'profileImage' ]))
{
// strip all possible redundant paths
// you should probably sanitize even more (check valid extensions etc.)
$profileImage = basename($_GET[ 'profileImage' ]);
if($someConditionsThatVisitorIsAllowedToViewThisImageAreMet)
{
// presuming mime type jpeg for now
header('Content-Type: image/jpeg');
readfile($dir . '/' . $profileImage);
exit();
}
else
{
// conditions are not met, dish out HTTP/1.1 403 Forbidden header
header('HTTP/1.1 403 Forbidden', true);
exit();
}
}