Page 2 of 2
Posted: Sat Jan 24, 2004 8:38 pm
by foyleman
PERFECT!!!!!!
I played around with it for a while and found the perfect solution.
Code: Select all
Ignore_User_Abort(True); //necessary to make sure the script finishes running to check for stop
$start_time = time();
// I created a blank file that could be written
$fo = fopen("stream_result.txt", "wb");
fwrite($fo, "started...\n");
fclose($fo);
echo "running";
$previous = 0;
While(!Connection_Aborted()) {
if((time() - $start_time) != $previous) {
echo "."; // print a period every second
$previous++;
}
Flush(); //Now php will check the connection
}
if(connection_status() != 0) {
$fo = fopen("stream_result.txt", "ab");
fwrite($fo, "stopped after ".(time() - $start_time)." seconds\n");
fclose($fo);
}
This will write to a file (that must already be created on the server) the number of seconds the script was running.
Posted: Sat Jan 24, 2004 9:25 pm
by foyleman
Maybe you can help me with ftell() for the script. Here's the entire test script that I have so far (hopefully help someone else in the future) :
Code: Select all
Ignore_User_Abort(True);
$start_time = time();
$filename = "files/cod/suppliments/Allied_AI.zip";
$ext = substr( $filename,-3 );
if( $filename == "" ) {
echo "<html><body>ERROR: Empty file to download. USE download.php?file=їfile path]</body></html>";
exit;
} elseif ( ! file_exists( $filename ) ) {
echo "<html><body>ERROR: File not found. USE download.php?file=їfile path]</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));
$fo = fopen("stream_result.txt", "wb");
fwrite($fo, "started file size = ".filesize($filename)."\n");
fclose($fo);
While(!Connection_Aborted()) {
$handle = fread($filename, filesize($filename));
$read = ftell($handle);
print($handle);
flush();
}
if(connection_status() != 0) {
$fo = fopen("stream_result.txt", "ab");
fwrite($fo, "stopped after ".(time() - $start_time)." seconds at byte $read");
fclose($fo);
}
My problem is in determining how much of the file has been transfered. I thought I might be able to use ftell($handle) to tell me where the current pointer is in the file transfer, but the value continues to be empty... as if ftell() doesn't work or perhaps I don't really know how to use it.
Basically... in the above script, is there any way to determine how much of the file has been transfered?
Posted: Sun Jan 25, 2004 12:08 am
by Gen-ik
Thanks for the code guys.
Another load of useful functions that I seem to have somehow missed in the manual

Oh well, you live and you learn.