Force Download in PHP

0 min read 45 words

This script works in all browsers, including Internet Explorer! 🙂

if (strstr($_SERVER['HTTP_USER_AGENT'],"MSIE")) {
  header("Pragma: public");
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Cache-Control: private",false);
  header("Content-Type: application-download");
  header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
  header("Content-Transfer-Encoding: binary");
  header("Content-Length: ".@filesize($ab_file));
  set_time_limit(0);
} else {
  header("Content-type: application-download");
  header("Content-Length: ".filesize($ab_file));
  header("Content-Disposition attachment; filename=".$filename);
}
readfile($ab_file);
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags