Force File Download Using PHP

A while ago I was asked to create a method to force files to download when a user requested. More importantly MP3 files, as when a user clicked on an MP3 file it would simply play in the browser. Yes why not right click / save as… etc etc well there were language barriers between us and some other people who were receiving the files.

This led me to create the a script to force mp3 download which I then tweaked further to force any file to download. Granted I had some help over at Experts Exchange but the script I have below will do all of the following.

  • force asf downloads
  • force avi downloads
  • force doc downloads
  • force zip downloads
  • force xls downloads
  • force gif downloads
  • force mp3 downloads
  • force mpeg downloads
  • force rtf downloads
  • force htm and html downloads
  • force asp downloads

The full script to do all of this is below, as a guide I’d copy and save it as download.php in your websites root folder.

Force File Download Script

<?php
if(isset($_REQUEST["id"])){ $id = $_REQUEST["id"]; }

# the files to download
switch ($id) {
    case '2f7b5a2624':
          $strFilePath =  './file-location/filename.doc';
          break;
     case '54707b2838':
          $strFilePath =  './file-location/filename.mp3';
          break;
     default:
        header('Location: http://www.YOUR-HOMEPAGE.com');
}
# end of the files to download

$strFileSize = filesize($strFilePath);
$strFileName = basename($strFilePath);

$pathinfo = pathinfo($strFilePath);
$strFileType = $pathinfo['extension'];

switch ($strFileType) {
     case 'asf':
          $ContentType = 'video/x-ms-asf';
          break;
     case 'avi':
          $ContentType = 'video/avi';
          break;
     case 'doc':
          $ContentType = 'application/msword';
          break;
     case 'zip':
          $ContentType = 'application/zip';
          break;
     case 'xls':
          $ContentType = 'application/vndms-excel';
          break;
     case 'gif':
          $ContentType = 'image/gif';
          break;
     case 'jpg':
          $ContentType = 'image/jpeg';
          break;
     case 'wav':
          $ContentType = 'audio/wav';
          break;
     case 'mp3':
          $ContentType = 'audio/mpeg3';
          break;
     case 'mpg':
     case 'mpeg':
          $ContentType = 'video/mpeg';
          break;
     case 'rtf':
          $ContentType = 'application/rtf';
          break;
     case 'htm':
     case 'html':
          $ContentType = 'text/html';
          break;
     case 'asp':
          $ContentType = 'text/asp';
          break;
     case 'mov':
          $ContentType = 'video/quicktime';
          break;
     case 'txt':
          $ContentType = 'text/plain';
          break;
     default:
          //Handle All Other Files
          $ContentType = 'application/octet-stream';
}

header("Content-Type: $ContentType");
header("Content-Length: $strFileSize");
header("Content-Disposition:attachment; filename=$strFileName");

$fd = fopen ($strFilePath, "r");
while (!feof ($fd)) {
    $buffer = fgets($fd, 32768);
    echo $buffer;
}
fclose ($fd);
?>

The only bit I am going to explain is right at the top in the switch statements. This is where we list our files that we want to force download of. If we take a look closer to the top the page is ready to accept an ID in the url. If you have saved your file as download.php we can access a download using this

http://www.domain.com/download.php?id=YOURIDHERE

Where YOURIDHERE would be replaced with the id in the case below code. To download the below file you would refer to it using

http://www.domain.com/download.php?id=2f7b5a2624

and as it is a true match it would then exit the switch statement with the $strFilePath variable populated and ready for the rest of the code to process the forcing of the download.

    case '2f7b5a2624':
          $strFilePath =  './file-location/filename.doc';
          break;

The letters and numbers you put in the case ‘ ‘ are up to you, whatever you put here will need to be the same as the id= in the url to make this particular file download.

You can have as many files as you want listed here. or if you liked you could link it all to a MYSQL database. One thing to remember is that you do not have the same id= string.

We also have the following up the top there

     default:
        header('Location: http://www.YOUR-HOMEPAGE.com');

You will need to change this to a page on your site, or maybe even your 404 page. The default will pick up any attempts to get into the page using invalid id’s and with automatically forward them to the page you put here.

This script can handle very large filetypes with ease, I have used it to force downloads of files that are hundreds of meg in size.

Use this code it will serve you wisely, any thought or suggestions on this code then drop us a comment.

Shane

Share and Enjoy:
  • TwitThis
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Slashdot
  • StumbleUpon
  • Technorati
  • Live
  • MySpace
  • Pownce
  • Reddit
  • Tumblr

If you liked this then check out these

Tags: ,

Leave a Reply