How to force a file to be downloaded


  • Applies to: Grid
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: File Manager, FTP, or SSH access
  • Applies to: DV
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: File Manager, FTP, or SSH access
  • Applies to: WordPress Hosting
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: SFTP or SSH access

Overview

Overview

By default, when a file is requested through a URL, the contents will appear in the web browser. The following guide will show you how you can force a file to be downloaded when visited through its URL.

Instructions

In this example, we have a text file on our website. This file will be called test.txt.

  1. When this file is requested through a URL, the contents are displayed in the browser.

    1.png
  2. To change this, navigate to your website's root directory. root directory. root directory.
  3. Insert the following code into theĀ .htaccess file:
    AddType application/octet-stream txt
  4. Now, if we re-attempt to request the test.txt file through a browser, the file should be automatically downloaded.

    You may need to clear your caches in order for the change to applied.

    2.png

  5. To expand force downloads to include other extensions, the following code can be added to the .htaccess file:

    The code below will force .mov, .mp3, .jpg, and .pdf files to be downloaded.

    <FilesMatch "\.(mov|mp3|jpg|pdf)$">
    ForceType application/octet-stream
    Header set Content-Disposition attachment
    </FilesMatch>

Resources