PHP Archives

Downloading Files using PHP

Posted on April 18, 2020 at 10:28 PHP No Comments

Last week I revamped my anime collective and among the changes was the removal of the lightbox when downloading an avatar. Naturally came across countless solutions on the Innernet but nothing compared to how PHP Tutorial Point handled it. The fewest lines but the most readable!

$file = "vaccines/covid19.pdf";
header("Content-Type: " . filetype($file));
header("Content-Length: " . filesize($file));
header("Content-Disposition: attachment; filename=" . basename($file));
readfile($file);

Make sure that the path to the file is relative to the file where the code is.