Force File Download


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



Copy this code and paste it in your HTML
  1. if (file_exists("path/".$filename)) {
  2. header('Content-Description: File Transfer');
  3. header("Content-type: application/octet-stream");
  4. header("Content-Disposition: attachment; filename=\"".filename."\"\n");
  5. header('Content-Transfer-Encoding: binary');
  6. header('Expires: 0');
  7. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  8. header('Pragma: public');
  9. header('Content-Length: '.filesize("path/".$filename));
  10. flush();
  11. readfile("path/".$filename);
  12. }
  13.  
  14. // if readfile is blocked, try:
  15.  
  16.  
  17. if (file_exists("path/".$filename)) {
  18. header('Content-Description: File Transfer');
  19. header("Content-type: application/octet-stream");
  20. header("Content-Disposition: attachment; filename=\"".filename."\"\n");
  21. header('Content-Transfer-Encoding: binary');
  22. header('Expires: 0');
  23. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  24. header('Pragma: public');
  25. header('Content-Length: '.filesize("path/".$filename));
  26. flush();
  27. $fp=fopen("path/".$filename, "r");
  28. fpassthru($fp);
  29. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.