简体   繁体   English

如何拒绝用户访问,但从.htaccess启用php脚本

[英]how to deny access to user but enable to php script from .htaccess

I have a server which stores images in a folder called 'images' but I want it to only be accessed by the php scripts. 我有一台将图像存储在名为“ images”的文件夹中的服务器,但我希望只能由php脚本访问。 Can anyone help? 有人可以帮忙吗? Thank you. 谢谢。

In your images folder, create an htaccess file with: 在您的images文件夹中,使用以下命令创建一个htaccess文件:

Deny from All

Then in your php scripts, you do something like: 然后在您的php脚本中,您可以执行以下操作:

<?php
$file = 'images/image.jpg';

if (file_exists($file)) {
    header('Content-Type: image/jpeg');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

First, you block direct access with a simple .htaccess 首先,使用简单的.htaccess阻止直接访问

deny from all

You can use readfile to read the image from the filesystem and output the bytes to the HTTP response body. 您可以使用readfile从文件系统读取图像,并将字节输出到HTTP响应正文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM