How to configure Apache2 to download files directly?

30,893

Solution 1

You can force it to be downloaded by making Apache report it's content type to be application/octet-stream, instead of the usual text/html. This is not something I would recommend doing to .txt files. In your .htaccess (or other Apache configuration files) add:

AddType application/octect-stream .txt

Another approach is to use ForceType within a Files or a Directory block, as outlined in this SO answer:

<Files *.mp4>
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</Files>

The advantage is that the matching criterion for a Files block is more flexible than that of a plain AddType.

Solution 2

A text file can be read by apache and will be read and displayed by it, you can still save the file with the browser, though.

Try again, with another ending like text.zip. Doesn't even need to be a real zip, changing the ending will change the behaviour.

On a personal note: Make sure to secure your server with .htaccess and a user/password combination if these files are not for the public!

Share:
30,893

Related videos on Youtube

Lightn1ng
Author by

Lightn1ng

Updated on September 18, 2022

Comments

  • Lightn1ng
    Lightn1ng over 1 year

    :-)

    I have a server running Ubuntu Server 14.04, and I have the apache2 webserver running on it. I would like to have a personal website for me and a few friends to host a few files. I created the website and all is running smoothly. To test file hosting, I created a "test.txt" file in the /var/www/html directory and created a link on the page to the file. In the file, I had this string:

    This is a test file.
    

    When I click the link in my browser on my computer, however, instead of the file downloading, the string "This is a test file." appears in my browser. Tested on multiple browsers. Any suggestions? I am very new to website hosting... is there something I forgot? Thanks in advance.

    -Ryan