使用htaccess的授予目录访问前,用PHP来验证目录、htaccess、PHP

2023-09-02 00:54:50 作者:情动则心痛

我工作的一个应用程序,用户可以创建一个帐户,并上传图片,其中获得存储在目录中,然后可以在应用程序本身,或在应用程序的公开可见的部分显示出来。现在,我真的想保护的图像只允许访问这些图像在某些条件得到满足,即会话设置,或在DB的图像的权限被设置为公开,仅举几例事件

I'm working on an app where users can create an account and upload images, which get stored in a directory and can then be displayed within the application itself or in a publicly visible part of the app. Now I'd really like to protect the images to ONLY allow access to these images in the event that certain conditions are met, i.e, a session is set or the permissions in the db for the image are set to public, to name a few.

那么,我需要的是,只要从该目录中的映像加载,htaccess的文件通过图片名称到该目录下,运行任何检查它有一个PHP文件,如果返回true, htaccess的可以继续随地吐痰的图像(无论是在标签或刚刚进入,在浏览器的地址栏中)。

So, what I'd need is that whenever an image from that directory is loaded, the htaccess file passes the image name onto a php file in that directory, which runs whatever checks it has to, and if it returns true, htaccess can proceed with spitting out the image (whether it's in an tag or just entered in the address bar of the browser).

我已经经历了很多帖子受控,但没有发现任何东西。我无法想象这是不可能的,所以任何人谁可以提供指导,将祈祷 - !如果你是本地的,还有其他的好处可能是在商店

I've trolled through many posts but haven't found anything. I can't imagine it's not possible, so anyone who can offer guidance will be prayed for - and if you're local, well, other benefits may be in store!

推荐答案

存放上传的图片在非Web访问的文件夹,然后 使用重写规则将请求转发到PHP的;是这样的:重写规则^图像/([^ /] +)/image.php?img=$1 [NC]

Store the uploaded images in a non web-accessible folder, then Use a rewrite rule to forward requests to php; Something like: RewriteRule ^images/([^/]+) /image.php?img=$1 [NC]

做你的验证在PHP中,如果确定从转发通过PHP非可读的文件夹中的形象;像

Do your validations in the php and if ok forward the image from the non-readable folder via php; something like

header('Content-type: '.$mime);
header('Content-length: '.filesize($filename));
$file = @ fopen($filename, 'rb');
if ($file) {
  fpassthru($file);
  exit;
}