Php Flow » Php

function for force download a file on browser

Below function is used for download the file. This function will take file name and filepath as an argument.

  1. function force_download_file($file$filePath){  
  2. //set the file path  
  3.     $dir = $filePath;  
  4. //chek the file is exist or not  
  5.     if ((isset($file))&&(file_exists($dir.$file))) {  
  6. //set the content type  
  7.        header(“Content-type: application/force-download”);  
  8.        header(‘Content-Disposition: inline; filename=”‘ . $dir.$file . ‘”‘);  
  9.        header(“Content-Transfer-Encoding: Binary”);  
  10.        header(“Content-length: ”.filesize($dir.$file));  
  11.        header(‘Content-Type: application/octet-stream’);  
  12.        header(‘Content-Disposition: attachment; filename=”‘ . $file . ‘”‘);  
  13.        readfile(“$dir$file”);  
  14.     } else {  
  15.        echo “No file selected”;  
  16.     } //end if  
  17. }  

Did you enjoy this article? Share it!

About the Author:

Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>