Force download a file


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /********************
  3. *@file - path to file
  4. */
  5. function force_download($file)
  6. {
  7. if ((isset($file))&&(file_exists($file))) {
  8. header("Content-length: ".filesize($file));
  9. header('Content-Type: application/octet-stream');
  10. header('Content-Disposition: attachment; filename="' . $file . '"');
  11. readfile("$file");
  12. } else {
  13. echo "No file selected";
  14. }
  15. }
  16.  
  17.  
  18. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.