Force a file download

Last modified date

Comments: 0

Here’s a small function that will allow you to force a file download.

[php]/**
* Force a file download via HTTP.
*
* File is required to be on the same server and accessible via a path.
* If the file cannot be found or some other error occurs then a
* ‘204 No content’ header is sent.
*
* @param string $path Path and file name
* @param string $name Name of file when saved on user’s computer,
* null for basename from path
* @param string $type Content type header info (e.g., ‘application/vnd.ms-excel’)
* @return void
* @access public
*/
/* public static */ function download($path, $name = null, $type = ‘binary/octet-stream’)
{
if (headers_sent()) {
echo ‘File download failure: HTTP headers have already been sent and cannot be changed.’;
exit;
}

$path = realpath($path);
if ($path === false || !is_file($path) || !is_readable($path)) {
header(‘HTTP/1.0 204 No Content’);
exit;
}

$name = (empty($name)) ? basename($path) : $name;
$size = filesize($path);

header(‘Expires: Mon, 20 May 1974 23:58:00 GMT’);
header(‘Last-Modified: ‘ . gmdate(‘D, d M Y H:i:s’) . ‘ GMT’);
header(‘Cache-Control: no-store, no-cache, must-revalidate’);
header(‘Cache-Control: post-check=0, pre-check=0’, false);
header(‘Cache-Control: private’);
header(‘Pragma: no-cache’);
header(“Content-Transfer-Encoding: binary”);
header(“Content-type: {$type}”);
header(“Content-length: {$size}”);
header(“Content-disposition: attachment; filename=\”{$name}\””);
readfile($path);
exit;
}[/php]

Very easy to use, too! Here are some examples of how you might call the function:

[php]download(‘./myfile.txt’);

download(__FILE__, ‘a file for you.php’);

download(‘/home/you/files/spreadsheet.xml’, ‘ssheet_’ . date(‘Ymd’), ‘application/vnd.ms-excel’);[/php]

Share

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.