RE: [PHP] User aborting a download and counting the filesize the website transfered.

From: Date: Wed, 23 Oct 2002 23:57:14 +0000
Subject: RE: [PHP] User aborting a download and counting the filesize the website transfered.
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-121180@lists.php.net to get a copy of this message
> Is it possible to let a user download a file from your website but if he > cancel's it the script's gets aborted > or call's another function instead going on. > > I am asking this because i want to record downloaded filesize statistics > and i need to know if a transfer > is succeeded or aborted. > > It would even be nice if i could record the downloade filesize of the part > the user downloaded from my web > even if he didn't finished the download. I think you could use a combo of fread() and a check of connection_abort() to accomplish this. Something like this: <? //not guaranteed, check this for syntax errors. //set headers for type of file you're sending $filename = "whatever.mp3"; $fp = fopen($filename,"r"); $filesize = filesize($filename); $flag = 0; $sent = 0; while(!feof($fp)) && $flag == 0) { $data = fread($fp,1024); echo $data; $sent += 1024; if(connection_aborted()) { $flag = 1; echo "sent $sent bytes of $filesize bytes"; } } if($flag == 0) { echo "sent $filesize bytes"; } ?> You can modify the 1024 to your liking. The smaller it is, the more "exact" you'll be with how much data you actually sent, but it may be slower. Use the connection_aborted() loop to store only the amount actually sent, and the flag check at the end to store that the entire file was sent. Again, try at your own risk, but I think that would work. ---John Holmes...

« previous php.general (#121180) next »