简体   繁体   中英

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

只需将它们上传到您的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

Put the in a separate directory, and in that directory create a file called .htaccess containing the following:

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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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