File download header Issue??
Posted on 2004-11-01
I have a php function that is meant to download a file when I post the filename to a new page. Basically I have a file browser window that I've created to securely access certain files outside of the webroot. I can get the files names pass the files over and they will download. The problem I am having is this:
There will be 2 save or open screens when I download the file before it will actually download. The other problem is I can't get the processing page to close itself once the download is complete. I have included the code I am using to do the download. Can anybody tell me what is wrong with this code that is causing it to react the way it is? Are the headers somehow wrong or is it a windows issue?
function DownloadFile($filename,$Download = 1)
// Check filename
if (empty($filename) || !file_exists($filename))
// Create download file name to be displayed to user
$saveasname = basename($filename);
// Fix for SSL in IE
header('Content-Type: application/octet-stream'); // Send binary filetype HTTP header
// If you want it to just open in the browser instead of prompting to download,
// send the proper content type.
header('Content-Type: application/pdf'); // Send PDF filetype HTTP header
// Send content-length HTTP header
// Send content-disposition with save file name HTTP header
// (using workaround for MSIE 5.5 SP1 / MSIE 6.0 bugs/problems)
if (IsSet($HTTP_USER_AGENT) && (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) || preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)))
header('Content-Disposition: attachment; filename="'.$saveasname.'"');
header("Content-Disposition: inline; filename=\"" . $saveasname . "\"");
// Send Content-Transfer-Encoding HTTP header
// Output file