How to Use PHP to Force a File Download
Download a file rather viewing in the browser. It’s easy method, to implement a force download using PHP.
Below script, we are using PHP headers to download a file.
header(
"Pragma: public"
, true);
header(
"Expires: 0"
);
// set expiration time
header(
"Cache-Control: must-revalidate, post-check=0, pre-check=0"
);
header(
"Content-Type: application/force-download"
);
header(
"Content-Type: application/octet-stream"
);
header(
"Content-Type: application/download"
);
header(
"Content-Disposition: attachment; filename="
.
basename
(
$file
));
header(
"Content-Transfer-Encoding: binary"
);
header(
"Content-Length: "
.
filesize
(
$file
));
die
(
file_get_contents
(
$file
));
Did this post help you?
Tutsplanet brings in-depth and easy tutorials to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply