How to allow downloading files with PHP, but deny

2019-06-09 23:10发布

I am using Apache on my server and I'd like to allow my visitors to download resources free of charge. However, to preserve even a little bit of bandwidth I'd like to deny direct access to the root folder of the resources like this:

www.myhost.com/resources/file.wav

If the visitor removes file.wav from the URL, they have access to all sounds at once and thus I'd have people just downloading like crazy. I don't want that.

How can I stop the users from going into that root directory, and any subfolders?

1条回答
再贱就再见
2楼-- · 2019-06-09 23:45

The dead easiest way to do this, without even messing with .htaccess (though that is a good idea) is to simply put an index.html file in the folder and set its permissions to 700.

If you want to just turn off directory listings you can create an htaccess file with this:

<Directory /path/to/directory>
   Options -Indexes
</Directory>

If you want to deny access to sub-directories, you can use this:

<Files subdirectory/*>
    deny from all
</Files>

If you want to allow access to just the .wav files, you can do this:

<Files *.wav>
    allow from all
</Files>
查看更多
登录 后发表回答