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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.