我正在开发一个应用程序,用户可以在其中创建帐户并上传图像,这些图像存储在目录中,然后可以在应用程序本身或应用程序的公开可见部分中显示。现在我真的想保护图像只允许在满足某些条件的情况下访问这些图像,即设置会话或将图像的数据库中的权限设置为公共,仅举几例
所以,我需要的是,每当加载来自该目录的图像时,htaccess文件将图像名称传递到该目录中的php文件,该文件运行它所具有的任何检查,如果它返回true, htaccess可以继续吐出图像(无论是在标签中还是只是在浏览器的地址栏中输入)。
我已经通过很多帖子,但没有找到任何东西。我无法想象这是不可能的,所以任何可以提供指导的人都会被祈祷 - 如果你是本地人,那么,其他好处可能会存在!
答案 0 :(得分:3)
将上传的图像存储在非Web可访问的文件夹中
使用重写规则将请求转发给php;类似于:RewriteRule ^images/([^/]+) /image.php?img=$1 [NC]
在php中进行验证,如果可以通过php从不可读的文件夹转发图像;
之类的东西header('Content-type: '.$mime);
header('Content-length: '.filesize($filename));
$file = @ fopen($filename, 'rb');
if ($file) {
fpassthru($file);
exit;
}
答案 1 :(得分:1)
据我了解您的情况,您需要通过.htaccess
关闭对文件的直接访问拒绝所有
并在另一个目录中创建一个PHP前端,它将从$ _GET获取路径或图片文件名并检查是否满足条件并返回图像,例如
header("Content-type: image/jpeg");
readfile("/path/to/my/images/$image");
答案 2 :(得分:1)
如果我是你,我会将你的所有图像移动到某个文件夹,然后构建一个像这样的php脚本image.php:
<?php
// You may want to use image name depending on
// if you can fetch the name from the db from an id
// You need the filename to open the image later
$iid = 0;
if(isset($_GET['iid']))
$iid = (int)$_GET['iid'];
// Check session
// Check database permissions ect.
// If you don't want this image to be output call die();
// If it's a go, output the image.
// Change this to the type of the image you're about to output
header('Content-Type: image/png');
readfile('images/'.$image_filename);
?>
您可以将.htaccess设置为:
RewriteRule ^images/(.*)$ image.php?iid=$1 [L]
因此,当为images/10.jpg
发出请求时,它会使用iid=10.jpg
您可以稍微更改.htaccess,具体取决于您是否要使用ID或文件名等,您可能不希望传递.jpg。与脚本相同。如果您有任何问题,请告诉我们。)