简体   繁体   English

如何防止直接访问文件?

[英]How to prevent direct access to files?

I have a system where users upload image files. 我有一个用户可以上传图像文件的系统。 I only want specific users to have access to these files and not for anyone to just go to example.php/image.jpg and see any user's image...Thanks 我只希望特定用户可以访问这些文件,而不希望任何人都可以转到example.php / image.jpg并查看任何用户的图像...谢谢

只需将它们上传到您的Web根目录之外的目录中,并使用php脚本来获取它们,并在用户满足适当要求时将其显示给用户。

You can create directory for each user. 您可以为每个用户创建目录。 For example, if username is Jhon, you can create a directory images/jhon/, then use PHP to restrict access to another users directory 例如,如果用户名是Jhon,则可以创建目录images / jhon /,然后使用PHP限制对其他用户目录的访问

Put the in a separate directory, and in that directory create a file called .htaccess containing the following: 将其放在单独的目录中,然后在该目录中创建一个名为.htaccess的文件,其中包含以下内容:

deny from all

This will do what you want. 这将做您想要的。

You can add the following to an .htaccess file, and drop the file into the directory where your images are stored : 您可以将以下内容添加到.htaccess文件中,然后将该文件放入存储图像的目录中:

To prevent directory browsing: 为了防止目录浏览:

Options All -Indexes

To prevent image hotlinking: 为防止图像热链接:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|js|css)$ - [F]

Replace mydomain.com with your own domain name. 用您自己的域名替换mydomain.com。

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

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