What am I missing?
The code below should begin by echoing to the screen "PROCESSING. . . . " Then the user should be forced to download "filename". If the user cancels or the download finishes, then a text file and a database table are updated. Then the "find_stat" function should echo to the screen "FAILED" or "COMPLETED". Then the page should be finished.
<?
ob_start(); //OPEN FIRST BUFFER
function print_to_screen() //FUNCTION TO NOTIFY USER THAT PAGE IS PROCESSING
{
ob_start();
echo "PROCESSING. . . . . .";
ob_end_flush();
}
Function download_file() //FORCE DOWNLOAD OF FILE
{
ob_start();
Ignore_User_Abort(True); // finish the script after it starts running
$start_time = time();
$filename = "IPTV_Viewer_Setup.exe";
$ext = substr( $filename,-3 );
if( $filename == "" ) {
echo "<html><body>ERROR: Empty file</body></html>";
exit;
} elseif ( ! file_exists( $filename ) ) {
echo "<html><body>ERROR: File not found</body></html>";
exit;
};
switch( $ext ){
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: $ctype");
$user_agent = strtolower ($_SERVER["HTTP_USER_AGENT"]);
if ((is_integer (strpos($user_agent, "msie"))) && (is_integer (strpos($user_agent, "win")))) {
header( "Content-Disposition: filename=".basename($filename).";" );
} else {
header( "Content-Disposition: attachment; filename=".basename($filename).";" );
}
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
// write the results to a file
$fo = fopen("stream_result1.txt", "wb");
fwrite($fo, "started file size = ".filesize($filename)."\n");
fclose($fo);
$fr = fopen($filename, 'rb');
while(!feof($fr)) {
$handle = fread($fr, filesize($filename));
print($handle); // send the file being read
flush();
}
// write the results to a file
if(connection_status() != 0) {
global $constat=1;
$status=0;
mysql_pconnect("localhost", "dbadmin", "tech") or die("Unable to connect to SQL server");
mysql_select_db("test") or die("Unable to select database");
$sql = "UPDATE download_test SET status='$status' WHERE id='$id' AND file_id='$file_id'";
$result = mysql_query($sql)or die(mysql_error());
$fo = fopen("stream_result2.txt", "ab");
fwrite($fo, "stopped after ".(time() - $start_time)." seconds -" . $file_id . "- " . $id . "\n\n");
fclose($fo);
}
else {
global $constat=2;
$status=0;
mysql_pconnect("localhost", "dbadmin", "tech") or die("Unable to connect to SQL server");
mysql_select_db("test") or die("Unable to select database");
$sql = "UPDATE download_test SET status='$status' WHERE id='$id' AND file_id='$file_id'";
$result = mysql_query($sql)or die(mysql_error());
$fo = fopen("stream_result3.txt", "ab");
fwrite($fo, "completed after ".(time() - $start_time)." seconds -" . $file_id . "- " . $id . "\n\n");
fclose($fo);
}
fclose($fr);
ob_end_flush(); //END OF SECOND OB_START
}
print_to_screen(); //NOTIFY USER THAT THE PAGE IS PROCESSING
download_file(); //FORCE DOWNLOAD OF FILE
function find_stat($constat){
if ($constat==1){echo "DOWNLOAD FAILED "; echo $constat;}
else {echo "DOWNLOAD COMPLETED"; echo $constat;}
}
find_stat($constat); //NOTIFY USER THAT THE FILE FIALED OF COMPLETED
ob_end_flush(); //END OF FIRST OB_START
?>
So far the only thing that the code does is force the file download. What about the rest? Why is it not running the other fuctions?
SYSTEM SPECs: redhat ES, MySQL 4.3, APACHE 2.0, PHP 4
Any help would be great....
Thanks,
Sam