Here you’ll learn how to force download any file using php header() and readfile() function.

Now a days web browsers are become more advance, Suppose you have a html or pdf file and you just put the link of that file in href=”FILE_NAME” then the web browsers open these files automatically. instead you need to do write little script in php to force download them.

PHP gives privilege you to change the HTTP headers of files that you are reading and writing, so that you can force a file to be downloaded that. So you can use this method for files like PDFs, document files, images, and video that you want your customers to download rather than read online.

Create your force download function in any where you want copy and paste below code

function forceDownload($file) {
 //Check file exist or not
  if (file_exists($file)) {
     if(ini_get('zlib.output_compression')) { 
     	// required for IE
                ini_set('zlib.output_compression', 'Off');  
        }
 
        // Get mine type of file.
   $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
   $mimeType = finfo_file($finfo, $file) . "\n";
   finfo_close($finfo);
        header('Expires: 0');
        header('Pragma: public'); 
        header('Cache-Control: private',false);
        header('Content-Type:'.$mimeType);
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Length: '.filesize($file));
        header('Connection: close');
        readfile($file);
        exit;
  } else {
   return "File does not exist";
  }
}




After that call this function where you want to integrate force download feature.

if(isset($_REQUEST['file']) && !empty($_REQUEST['file'])) {
   forceDownload($_REQUEST['file']); 
}
<form action="" method="post">
<input type="hidden" name="file" value="download/panda.jpg">
<button type="submit">Force Download</button>
</form>

Now your final file will be…

<?php
function forceDownload($file) {
 //Check file exist or not
  if (file_exists($file)) {
     if(ini_get('zlib.output_compression')) { 
     	// required for IE
                ini_set('zlib.output_compression', 'Off');  
        }
 
        // Get mine type of file.
   $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
   $mimeType = finfo_file($finfo, $file) . "\n";
   finfo_close($finfo);
        header('Expires: 0');
        header('Pragma: public'); 
        header('Cache-Control: private',false);
        header('Content-Type:'.$mimeType);
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Length: '.filesize($file));
        header('Connection: close');
        readfile($file);
        exit;
  } else {
   return "File does not exist";
  }
}
 
if(isset($_REQUEST['file']) && !empty($_REQUEST['file'])) {
   forceDownload($_REQUEST['file']); 
}
 
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Force download script in php</title>
</head>
<body>
<h3>Force download script in php</h3>
<form action="" method="post">
<input type="hidden" name="file" value="download/panda.jpg">
<button type="submit">Force Download</button>
</form>
</body>
</html>

DEMO DOWNLOAD

Hope this simple php snippet will help you to integrate force file download feature in your web based projects.

If you like this post please don’t forget to subscribe my public note book for more useful stuff