想象一下这种情况
- 用户上传图片给大家看(图片是公开的)
- 用户决定将图片设为私有(private)
- 图片只有他能看到,因为图片在数据库中设置为私有(private)。
这里有一个问题:
其他用户仍然可以从图片的绝对路径访问图片。 有什么办法可以避免这种情况吗?
最佳答案
将图像存储在网络根目录之外,并有一个 PHP 脚本来确定当前用户是否有权访问它,如果有则返回图像内容,否则返回 403 禁止错误。
您可以使文件看起来就像您在提供原始图像一样:
使用这个.htaccess
:
RewriteEngine on
RewriteRule images/(.*) image.php?file=$1 [L]
那么你的image.php
可以是:
<?php
$file = $_GET['file'];
// use $file to look up file information, eg. in the database
if( $it_exists && $has_permission_to_view) {
readfile("/root/path/to/real/images/".$file);
exit;
}
else header("HTTP/1.1 403 Forbidden");
关于php - 有什么办法可以防止用户使用绝对路径看到私密图片吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23063823/