简体   繁体   English

使用apache / php拒绝直接访问文件夹中的任何文件

[英]Deny direct access to any files in a folder using apache/php

This might seem like a duplicate question but I have tried the solution given in the similar questions. 这似乎是一个重复的问题,但是我尝试了类似问题中给出的解决方案。

I want to limit access to files in a few folders on my apache server so that they can only be served through a php script to only the users logged in to the part of my system that should have access to any particular folder. 我想限制对我的apache服务器上几个文件夹中文件的访问,以使它们只能通过php脚本提供给仅登录到应该有权访问任何特定文件夹的系统部分用户。

Other solutions suggest using the .htaccess 其他解决方案建议使用.htaccess

deny from all
allow from 127.0.0.1

But that seems to deny the files from everywhere.Including the local php scripts. 但这似乎拒绝了各地文件,包括本地php脚本。

Im considering .htpasswd but that is an extra level of user credentials that needs to be handeled and I'd prefere to avoid that. 我正在考虑.htpasswd,但这是需要处理的额外级别的用户凭据,我希望避免这种情况。

How should I get around this? 我应该如何解决? Is there any better way of storing the files? 有没有更好的方法来存储文件?

htaccess cannot deny access to files via php using include/require functions. htaccess无法使用include / require函数拒绝通过php访问文件。 If your php include is not accessing the file then the problem is the path to the file. 如果您的php include无法访问文件,则问题在于文件的路径。

Somehow php was restricted by the 某种程度上php受限制

deny from all
allow from 127.0.0.1

to include and require files in the affected folders. 在受影响的文件夹中包含并要求文件。 This goes against what I can read from the apache documentation how this should work and I have no real answer why. 这与我可以从apache文档中读取的内容相反,我对此没有真正的答案。 The problem persisted both on my localhost using Xampp and on the webhotel one.com. 该问题在使用Xampp的本地主机和webhotel one.com上均持续存在。

My original question was badly formed since I used require to test if I had read access to the folder when I actually wanted to use readfile. 我最初的问题格式很糟糕,因为当我实际想要使用readfile时,我曾使用require来测试是否对该文件夹具有读取权限。

My main concern was that I wanted to serve files for download from an inaccessible directory. 我主要担心的是,我想提供文件以便从无法访问的目录下载。 And the

readfile($filename);

could still access the files. 仍然可以访问文件。

The soultion to this problem was that I realized that readfile() and include() somhow gets different permissions. 解决此问题的要点是,我意识到readfile()和include()会获得不同的权限。 I could not find this in any documentation. 我在任何文档中都找不到。

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

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