简体   繁体   English

仅允许通过代码使用.htaccess访问文件夹

[英]Only Allow To Access Folders Via Code With .htaccess

I want to hide my uploads folder but i want to access it via php . 我想隐藏我的uploads文件夹,但我想通过PHP访问它。 Is this possible with .htaccess ? 这可能与.htaccess吗? I tried something but didn't worked. 我尝试了一些东西,但没有奏效。

    <files "/uploads">
  order allow,deny
  deny from all
    </files>

    <folders uploads>
Order Allow,Deny
Deny from all
    </folders>

You're almost there, but it depends on what version of Apache you're using also. 你几乎就在那里,但这取决于你正在使用的Apache版本。

The above method you're trying is if you want to block access to a specific file, if you want to block a folder, then add your .htaccess file to that folder and just use: 您正在尝试的上述方法是,如果要阻止对特定文件的访问,如果要阻止文件夹,则将.htaccess文件添加到该文件夹​​,然后使用:

Below 2.4: 低于2.4:

deny from all

2.4 or above: 2.4或以上:

Require all denied

IMPORTANT EDIT 重要编辑

You can just upload a .htaccess to the folder that you want to block with the following: 您可以使用以下内容将.htaccess上传到要阻止的文件夹:

Deny from all

If there is some issue, add: 如果有问题,请添加:

Allow from 127.0.0.1

It worked well for me. 它对我很有用。

Original answer 原始答案

Try: 尝试:

<Directory "/uploads">
    Order allow,deny
    Deny from all
    Allow from 127.0.0.1
</Directory>

EDIT: 编辑:

The code above will deny all except the local ip (of your server). 上面的代码将拒绝除本地ip(服务器)之外的所有代码。

As thickguru said, it also depends on your apache version. 正如thickguru所说,它还取决于你的apache版本。 Here are some other ways to do it: 以下是其他一些方法:

<Directory "/uploads">
    Require local
</Directory>

It will only allow if requested by the server (your script or somewhat on the server). 它只允许服务器(您的脚本或服务器上的某些服务器)请求。

Or: 要么:

<Directory "/uploads">
    Require ip 127.0.0.1
</Directory>

The same as above, but using the local adress. 与上面相同,但使用本地地址。 You can also add other ips to it. 您还可以添加其他ips。 All the ips that you add there will be allowed to access the folder. 您在其中添加的所有ips都将被允许访问该文件夹。

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

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