Posted By

o0110o on 10/20/12


Tagged

php headers download function


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

o0110o


A function that forces a file to download.


 / Published in: PHP
 

This function will force a file to be downloaded. It accepts $_path as a parameter.

  1. function file_force_dl($_path) {
  2. $mime = 'application/force-download';
  3. header('Pragma: public'); // required
  4. header('Expires: 0'); // no cache
  5. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  6. header('Cache-Control: private',false);
  7. header('Content-Type: '.$mime);
  8. header('Content-Disposition: attachment; filename="'.basename($_path).'"');
  9. header('Content-Transfer-Encoding: binary');
  10. header('Connection: close');
  11. readfile($_path); // push it out
  12. exit();
  13. }

Report this snippet  

You need to login to post a comment.