Posted By

kumar_sekhar on 04/14/11


Tagged

php file download inforce


Versions (?)

File download inforce script


 / Published in: PHP
 

simple pass the file name and that file will be available for download

  1. // find the file name.
  2. $filename = "invoice/filename';
  3.  
  4.  
  5. // required for IE, otherwise Content-disposition is ignored
  6. if(ini_get('zlib.output_compression'))
  7. ini_set('zlib.output_compression', 'Off');
  8.  
  9. // addition by Jorg Weske
  10. $file_extension = strtolower(substr(strrchr($filename,"."),1));
  11.  
  12.  
  13. switch( $file_extension )
  14. {
  15. case "pdf": $ctype="application/pdf"; break;
  16. case "exe": $ctype="application/octet-stream"; break;
  17. case "zip": $ctype="application/zip"; break;
  18. case "doc": $ctype="application/msword"; break;
  19. case "xls": $ctype="application/vnd.ms-excel"; break;
  20. case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  21. case "gif": $ctype="image/gif"; break;
  22. case "png": $ctype="image/png"; break;
  23. case "jpeg":
  24. case "jpg": $ctype="image/jpg"; break;
  25. default: $ctype="application/force-download";
  26. }
  27. header("Pragma: public"); // required
  28. header("Expires: 0");
  29. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  30. header("Cache-Control: private",false); // required for certain browsers
  31. header("Content-Type: $ctype");
  32. // change, added quotes to allow spaces in filenames, by Rajkumar Singh
  33. header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
  34. header("Content-Transfer-Encoding: binary");
  35. header("Content-Length: ".filesize($filename));
  36. readfile("$filename");
  37. exit();

Report this snippet  

You need to login to post a comment.