4 Simple Ways To Force Download Files Instead of Open In Browser On Server & WordPress Site | PHP Scripts & Other Methods

Follow Us on Social Media

Use These Simple Methods To Force Download Files On Server & WordPress Site Instead Of Opening In Browser | PHP Scripts & Other Methods

4 Simple Ways To Force Download Files Instead of Open In Browser On Server & WordPress Site | PHP Scripts & Other Methods


Instead of automatically initiating a download process when clicked, certain file links end up opening the files in another browser tab. An audio file may even begin to play in another browser tab when clicked. While you tried to download the file, it starts playing instead.

If you offer files as a digital download, this is definitely not what you want.

On WordPress, files such as audio which are uploaded using the WordPress media manager will often download automatically when clicked. 

The problem with automatic audio and files download on WordPress starts when such files are uploaded directly to the server either via FTP, cPanel or through any file manager on the web host or WP admin.

You want to solve this problem so your site visitors can download files when they click on the link instead of the link opening in another tab.


Below are some of the simple ways to solve this problem:

1. Solve the problem through a plugin: If you are using WordPress or other CMSs, there may be a plugin that manages digital downloads for your platform. For WordPress, there are many of these plugins and some of the most popular include WordPress Download Manager, Easy Digital Download, Download Monitor, etc.

If you are using this solution, you would create your download links by uploading files through the plugins which will automatically create a download link for you. Some of them also provide a way to paste a link to a file on the server or an external file. So you could paste the link to the file and create the download link which will automatically trigger a download when the links are clicked.

2. Solve the Problem by Creating an Archive of the File(s): Another simple method to solve the download problem is to create an archive of the file(s). An archive is simply a way of compressing the file or files together. You could compress to a zip, rar, tar, gz, etc file formats. To compress these files, you will need a program that can handle this on your computer or device. The most popular of this on Windows, Mac & Linux include Winrar, Winzip and others.

The downside to this solution is that people downloading your file will also need one of these programs to extract the file(s). The good news however, is that new versions of Windows come with a default program to extract some of the most popular compression formats like zip. Your OS may have something similar to the stock Windows file extractor also. 

3. Edit the .htaccess: This method is for those who are a little bit techie. You'll need to access the .htaccess located in your upload directory. For WordPress, this file is located in baseurl/wp-content/uploads.

When you find the .htaccess file, you'll add the following script. If you can't find the .htaccess here, you can create it. Just create a file and name it .htaccess and paste in the code. You can access the directory via FTP, server file manager, cPanel file manager or any WP file manager if you use WordPress.



#Force file download
<FilesMatch "\.(?i:mp3)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch>

#End

You'd use the code above if you wish to force download mp3 files.

To force download pdf files, you'll use this one below:

#Force file download
<FilesMatch "\.(?i:pdf)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch>
#End

Show your company is legit, from $36.88


Just replace the file format you wish to force download with the one in the code above.


If you wish to force download multiple file types, you can use the below code:

# Force file download 
<FilesMatch "\.(jpg|jpeg|png|gif|mp3|mp4|mpg|mpeg|avi|txt|ogg|wav)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch> 

# END

You can add other file formats that are not already in the code.

4. Force File Download Using HTML: HTML 5 allows users to force download using a simple code. You can force download by using the download keyword in your <a href> tag. To force download a file, open the text/code editor or directly edit the file URL and add the keyword download.

So for instance, if the title of the file is 'Some-awesome-file.mp3', to force download using this method, we'll have:

<a href="Some-awesome-file.mp3" download>Download Mp3</a> You can replace "Some-awesome-file.mp3" with your file path.


No comments



Powered by Blogger.