PHP Force a File to Download

Here’s how to force the browser to download a file:

<?php
$file = "/path/to/foo.jpg";
$filename = basename($file);
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$filename.'";');
readfile($file);
exit;
?>

Leave a Reply

Your email address will not be published. Required fields are marked *