[advanced] Download Tracking

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

foyleman
Forum Newbie
Posts: 3
Joined: Sat Jan 24, 2004 6:18 pm

Post 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.
foyleman
Forum Newbie
Posts: 3
Joined: Sat Jan 24, 2004 6:18 pm

Post 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=&#1111;file path]</body></html>";
   exit;
&#125; elseif ( ! file_exists( $filename ) ) &#123;
   echo "<html><body>ERROR: File not found. USE download.php?file=&#1111;file path]</body></html>";
   exit;
&#125;;
switch( $ext )&#123;
   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";
&#125;
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&#1111;"HTTP_USER_AGENT"]);
if ((is_integer (strpos($user_agent, "msie"))) && (is_integer (strpos($user_agent, "win")))) &#123;
   header( "Content-Disposition: filename=".basename($filename).";" );
&#125; else &#123;
   header( "Content-Disposition: attachment; filename=".basename($filename).";" );
&#125;
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()) &#123;
	$handle = fread($filename, filesize($filename));
	$read = ftell($handle);
	print($handle);
	flush();
	&#125;

if(connection_status() != 0) &#123;
	$fo = fopen("stream_result.txt", "ab");
	fwrite($fo, "stopped after ".(time() - $start_time)." seconds at byte $read");
	fclose($fo);
	&#125;

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?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
Post Reply