简体   繁体   中英

How to allow downloading files with PHP, but deny direct access to directory listings

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?

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>

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