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);


  1. Wow amazing i saw the article with execution models you had posted. It was such informative. Really its a wonderful article. Thank you for sharing and please keep update like this type of article because i want to learn more relevant to this topic.

    Web Designing Training in Chennai


  2. Helpful as always. Every post you write produce a massive value to your readers that is the only reason it is so popular and has great authority. I read this content really awesome.I hope this information may change my business carrier.I can remember these things whenever taking the decision.

    SAP training in Chennai

    1. Oh you like that it can't download any songs


  3. Really very nice blog information for this one and more technical skills are improve,i like that kind of post.

    SAP training in Chennai

  4. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us.

    SAP training in Chennai


  5. Really very nice blog information for this one and more technical skills are improve,i like that kind of post.

    Digital Marketing Training in Chennai

  6. Really thanks for sharing your valuable information, it is really useful to help and understand.
    PHP Training in chennai

  7. You have shared very informative news with. Thanks for your effort.php training classes

  8. This comment has been removed by a blog administrator.

  9. I have read your blog its very attractive and impressive bouncing balls. I like it your blog red ball.


Post a Comment

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.