Search code examples
php.htaccessmod-headers

Automatically download files in specified folder regardless of type


How can I make it so that when a user visits a file in a specified folder, I can force the file to be downloaded?

I know it's possible to do this type by type using .htaccess and you can force a file to download using a php script, but how can I force download the file they visited within the specified folder without a php script?

Currently, when you view a file that the browser knows how to read, it reads the file instead of downloading it. I want it to automatically download all files in the folder instead of reading ones that the browser supports.


Solution

  • The "mod_headers" module should be available.. then you can add this to your .htaccess file.

    By type:

    <FilesMatch "\.jpg$">
        Header set Content-Disposition attachment
        Header set Content-Type application/octet-stream
    </FilesMatch>
    

    A specific file by name:

    <Files test.jpg>
        Header set Content-Disposition attachment
        Header set Content-Type application/octet-stream
    </Files>
    

    Put it to the .htaccess file in the specific folder with your file to download.

    For the whole folder leave out Files/FilesMatch directive.