I wrote a download script that does not work in either IE7 or Netscape browsers, although it works in IE6. Can anyone help me with why, or does anyone have any code that they know works?
Here is my download script.
<?php
ob_start();
session_start();
// Protect against reaching max execution time
set_time_limit(0);
// Force script termination if user aborts
ignore_user_abort(false);
include '/usr/local/apache/dbinclude/db.inc';
include 'include/error.inc';
include 'include/generalQuery.inc';
set_error_handler("errorHandler");
if (isset($_SESSION['bestBuyLogin'])){
// write and close the session
session_write_close();
// Open a connection to the DBMS
if (!($connection = @ mysql_pconnect($hostName, $username, $password)))
showerror();
if (!mysql_select_db($databaseName, $connection))
showerror();
// query the download table
generalQuery($connection, "downloads");
// get the results of the query from the session and unset the session
$result = $_SESSION['result'];
unset($_SESSION['result']);
// fetch the row
$row = @ mysql_fetch_assoc($result);
$fileDir = $row['file_path']; // supply a path name.
$fileName = $row['file_name']; // supply a file name.
$filePath = $fileDir . '/' . $fileName; // combine the path and file
// translate file name properly for Internet Explorer.
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")){
$filePath = preg_replace('/\./', '%2e', $filePath, substr_count($filePath, '.') - 1);
}
if(!$file = @ fopen($filePath,'rb')){
die("Cannot Open File!");
}
else {
//Close the session to allow for header() to be sent
header('Pragma: ');// leave blank to avoid IE errors
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-Description: File Transfer');
if ($row['file_type'] == "mp3"){
header("Content-Type: audio/mpeg");
}
header('Content-Transfer-Encoding: binary');
$header="Content-Disposition: attachment; filename=" . $fileName . ";";
header($header);
header('Content-length:' . (string)(filesize($fileName)));
sleep(1);
while(!feof($file)){
echo fread($file, 8092);
flush();
}
fclose($file);
}
ob_end_flush();
}
?>