您可以使该文件夹无法从网络访问(例如,将文件夹放置在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 –