I tried using the ob_end_clean function, and even with the @ infront of it, my Firefox browser gives this error message. When I added @ob_start() in the line above it, I was back to the situation I had originally, cancel not reported correctly.
So that we're all clear about what I'm talking about, there are two points where the client can cancel. One in the file save window and the other is during the download itself (different graphics for IE and FireFox etc. so no pics.)
My goal is to detect anything that stops the download from completing (client generated or otherwise).
My web hoster is running
OS Linux
Apache 1.3.37 (Unix)
PHP 4.4.3
I've included my code below. (In my opening post of this thread, I didn't show the complete code I'm using, only the part where I thought the problem was at.)
The relevant section of code is included below in the order that it is executed (something I'ld like to do to the code myself).
(If you're familiar with osCommerce, you will recognise that I'm in the process of modifying some of it's functionality)
Here is a brief explaination:
download.php
creates a temp folder (starting with the '.' and puts a symlink (short-cut) into it that points to the actual file.
then calls tep_redirect_download.
general.php
tep_redirect_download adjusts the URL according to whether SSL is being used or not.
It loads a class called 'downloader' and calls the dl_resume function in it.
dl_resume returns a result code (that I'm using to debug this rotten thing)
class.download_dj.php
dl_resume does the actual download, and this is where I'm having the problems.
Please refer to my first post in this thread for an explaination of what I think is happening.
<?php
// This code is in a php script called "download.php"
tep_unlink_temp_dir(DIR_FS_DOWNLOAD_PUBLIC);
$tempdir = tep_random_name();
umask(0000);
mkdir(DIR_FS_DOWNLOAD_PUBLIC . $tempdir, 0777);
symlink(DIR_FS_DOWNLOAD . $downloads['orders_products_filename'], DIR_FS_DOWNLOAD_PUBLIC . $tempdir . '/' . $downloads['orders_products_filename']);
$download_status = tep_redirect_download(DIR_WS_DOWNLOAD_PUBLIC . $tempdir . '/' . $downloads['orders_products_filename']);
// Next lines allows me to see the result of the download using a number to indicate result. They is will be changed when everything is working.
if ($download_status == Null) {
$download_status = 111;
}
if ($download_status == 0) {
$download_status = 101;
}
tep_db_query("update " . TABLE_ORDERS_PRODUCTS_DOWNLOAD . " set download_count = $download_status where orders_products_download_id = '" . (int)$HTTP_GET_VARS['id'] . "'");
tep_exit();
// This code is in a php script called "general.php"
require(DIR_WS_CLASSES . 'class.download_dj.php');
////
// Download via redirected link & test for done-abort
function tep_redirect_download($url) {
if ( (ENABLE_SSL == true) && (getenv('HTTPS') == 'on') ) {
// We are loading an SSL page
if (substr($url, 0, strlen(HTTP_SERVER)) == HTTP_SERVER) {
// NONSSL url, therefore change it to SSL
$url = HTTPS_SERVER . substr($url, strlen(HTTP_SERVER));
}
}
$curr_user_abort = ignore_user_abort();
ignore_user_abort(true);
$dl = new downloader();
$xfer = $dl->dl_resume($url);
ignore_user_abort($curr_user_abort);
return $xfer;
}
// This code is in a php script called "class.download_dj.php"
class downloader
{
var $xfered = 0;
function dl_resume($path) {
@ob_start();
@ob_end_clean();
$speed_limit = 0;
//Caller must check if the file exists
//Gather relevent info about file
$filename = basename($path);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
$size=filesize($path);
if (connection_status()!=0) {
return 123;
}
if (connection_aborted()) {
return 1234;
}
if (isset($_SERVER['HTTP_RANGE'])) {
// Support for partial transfers enabled and browser requested a partial transfer
header("HTTP/1.1 206 Partial content\n");
$start = preg_replace(array("/(\040*|)bytes(\040*|)=(\040*|)/","/(\040*|)\-.*$/"),array("",""),$_SERVER['HTTP_RANGE']);
if ($size < $start) {
header("HTTP/1.1 411 Length Required\n");
echo "Trying to download past the end of the file. You have probably requested the wrong file. Please try again.";
return 456;
}
$transfer_size = $size - $start;
header("Accept-Ranges: bytes");
header("Content-Range: bytes ".$transfer_size."-".($size-1)."/".$size);
header("Content-Length:".$transfer_size."\n");
}
else {
header("HTTP/1.1 200 OK\n");
header("Content-Range: bytes 0-".($size-1)."/".$size);
header("Content-Length: ".$size);
}
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0", false);
header("Cache-control: private\n"); // fix for IE to correctly download files
header("Pragma: no-cache\n"); // fix for http/1.0
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H"), date("i"), date("s")+10, date("m"), date("d"), date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
switch( $file_extension ) {
case "mp3": $ctype="audio/mpeg"; break;
case "mpg": $ctype="video/mpeg"; break;
case "avi": $ctype="video/x-msvideo"; break;
case "wmv": $ctype="video/x-ms-wmv";break;
case "wma": $ctype="audio/x-ms-wma";break;
case "msi": $ctype="application/x-chap4.";break;
default: $ctype="application/force-download";
}
header("Content-Description: File Transfer");
header("Content-Type: " .$ctype);
header("Content-Disposition: inline; filename=".$filename);
header("Content-Transfer-Encoding: binary\n");
//open the file, if resumed then set position
$fp = fopen( $path, 'rb' );
if (isset($transfer_size)) {
fseek($fp,$transfer_size);
}
if ($speed_limit != 0) {
$chunk = $speed_limit * 1024;
while(!feof($fp) and (connection_status()==0)) {
echo fread($fp, $chunk);
$xfered = $xfered + $chunk;
flush();
}
}
else {
$chunk = 4096;
while(!feof($fp) and (connection_status()==0)) {
echo fread($fp, $chunk);
$xfered = $xfered + $chunk;
flush();
}
}
fclose($fp);
If ((connection_status()==0) and !connection_aborted()) {
return $xfered;
}
else {
return 12345;
}
} // end function dl_resume($path)
} // end class downloader
?>
Cheers,
Nap