In this post, we will learn how to make any MP3 and MP4 File force download using PHP? This will prevent streaming of these files in modern Browsers, which in turn may save your valuable account or CDN bandwidth.



1. Suppose your Domain Name is abcxyz.com. Create a new sub-domain, say downloads.abcxyz.com and upload all your MP3/MP4 files to the root directory, so that their direct address looks like downloads.abcxyz.com/test.mp3 and downloads.abcxyz.com/test.mp4.

2. In the same directory, create and upload a PHP file, say download.php, with following code in it:

<?php
$file = $_GET[‘file’];
header (“Content-type: octet/stream”);
header (“Content-disposition: attachment; filename=”.$file.”;”);
header(“Content-Length: “.filesize($file));
readfile($file);
exit;
?>

3. Now, use following HTML code for linking to your files:

<a href=”http://downloads.abcxyz.com/download.php?file=test.mp3″>Download TEST MP3</a>

When a visitor follows above download link, his browser will now ask where he want to save the media file on his computer’s hard drive?

Important Notes:

1. Instead of using a sub-domain, you can also use the root directory of your main domain i.e. abcxyz.com for hosting media files. But sometimes, existing CMS/.htaccess rules conflicts with above PHP Code. Either you will get a browser error or the media file will continue to stream in the browser.

It’s better to use a domain or sub-domain without any CMS installed with above PHP Trick.

2. If your server is using Linux like CentOS etc., then the name of your hosted media files will be case sensitive. For example, if you have uploaded XYZ.mp4, then you should link to it using its exact name:

<a href=”http://downloads.abcxyz.com/download.php?file=XYZ.mp4″>Download XYZ Video</a>

Following will not work OR may result in partial download:

<a href=”http://downloads.abcxyz.com/download.php?file=Xyz.mp4″>Download XYZ Video</a> OR <a href=”http://downloads.abcxyz.com/download.php?file=xyz.mp4″>Download XYZ Video</a>

3. Almost every hosting provider limits bandwidth for file download. If you see following entries anywhere in the Error Logs of your site, then there’s absolutely no need to worry:

[Sun Dec 22 01:20:37 2013] [warn] ID: 3 File: /home/ABCXYZ/public_html/downloads/test.mp3 Rate : 8000000 Minimum : 256 Size rate : 8000000 IP: 122.168.163.xx hostname: downloads.abcxyz.com
[Sun Dec 22 01:20:37 2013] [warn] ID: 3 File: /home/ABCXYZ/public_html/downloads/test.mp3 Rate : 8000000 Minimum : 256 Size rate : 8000000 IP: 122.168.163.xx hostname: downloads.abcxyz.com
[Sun Dec 22 01:20:37 2013] [warn] ID: 3 File: /home/ABCXYZ/public_html/downloads/test.mp3 Rate : 8000000 Minimum : 256 Size rate : 8000000 IP: 122.168.163.xx hostname: downloads.abcxyz.com
[Sun Dec 22 01:20:37 2013] [warn] ID: 3 File: /home/ABCXYZ/public_html/downloads/test.mp3 Rate : 8000000 Minimum : 256 Size rate : 8000000 IP: 122.168.163.xx hostname: downloads.abcxyz.com

Above warnings shows that the webhost is limiting the download speed of large media files to 8 Mbps. This helps in preventing certain users (using a very fast connection) from sucking down all the bandwidth of a server.