Create a download

To download a file instead of just displaying it requires multiple specific headers to be set.

$quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
$size = "blah" ; // determine filesize here

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted); 
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);


Determining the filesize can be done in one of two ways.

If your file is located on your local server, you can use theĀ filesize function.

$size = filesize($file);

Otherwise, if your file is a remote file, use the remote file's headers.

$size = array_change_key_case(get_headers($file, TRUE))["content-length"];