Skip to main content

Force File Download with PHP

If you want to control or track your downloadable contents then a force file downloader script can give you more option. Especially if you want make images or other stemming contents like music file (mp3, wav, ogg), video files (flv, mp4, ogg, 3gpp), css, JavaScripts or even a php file downloadable then you must need to use a force file downloader script.
In this post I am going to share a simple but useful force file downloader script.

Download Source

The Download Function

 * force file to download
 * @param string path to file
function download($path)
 // if file is not readable or not exists
 if (!is_readable($path))
  die('File does not exist or it is not readable!');

 // get file's pathinfo
 $pathinfo = pathinfo($path);
 // set file name
 $file_name = $pathinfo['basename'];

 // guess mime type by extension
 $mimes = require 'mimes.php';
 $mime = isset($mimes[$pathinfo['extension']]) ? $mimes[$ext] : 'application/octet-stream';

 // set headers
 header('Pragma: public');
 header('Expires: -1');
 header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
 header('Content-Transfer-Encoding: binary');
 header("Content-Disposition: attachment; filename=\"$file_name\"");
 header('Content-Length: ' . filesize($path));
 header("Content-Type: $mime");

 // read file as chunk to reduce memory usages
 if ( $fp = fopen($path, 'rb') ) {

  while( !feof($fp) and (connection_status()==0) ) {
   print(fread($fp, 8192));

Heavy memory usages is one of the most common issue of almost all force file downloader script. That's why I am not using functions like readfile. Because these files first stores whole file to the memory and then it starts download. If you want to try large files to download using this function then you will get memory limit exhausted problem. So, it is better to read a file as chunks.


$file = $_GET['file'];
download('path/'. $file);

Popular posts from this blog

Multiple File Upload with Progress Bar using PHP & jQuery

Some days age I have created a post that deals with How we can upload file with progress bar using php and jQuery . Some of my readers asked me how we can upload multiple files with progress bar. In this post I am going to show you how we can upload multiple files with progress bar. We can upload multiple files with progress bar exactly same way as I shown previous. But we need to do some modification with our file upload form and our php script to handle multiple files.

File Upload with Progress Bar using jQuery and PHP

In this post you will learn how to create a AJAX file uploading system with progress bar using jQuery and PHP. For this purpose we will going to use jQuery Form Plugin . It is a easy to use and powerful jQuery AJAX form submitting plugin. It supports XMLHttpRequest Level 2 and iframe file transportation. That's why it will work for both old and new browsers.

Multiple File Upload with PHP

Sometimes we need to allow our users to upload multiple file upload. On my previous post I was shown the basic of simple file upload with PHP. Today I am going to show you how to allow users to upload multiple files. It is almost similar like simple file upload but we need to do some modification with html markup and php code . multiple attribute is needed to add on the html markup. Major web browsers like Firefox, Chrome, Safari and IE 9+ support this attribute. Now I am going to show you how to upload multiple file with PHP and basic HTML form.