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 Comment

Yes, send me an email when a new comment is posted.

The avatars shown next to comments are Gravatars. Click here to get a Gravatar account for free and any other site that supports it will show your avatar too!