Re: User aborting a download and counting the filesize the website transfered.
| From: | Marek Kilimajer | Date: | Thu, 24 Oct 2002 09:33:15 +0000 |
| Subject: | Re: 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-121215@lists.php.net to get a copy of this message | ||
Also use flush() after echo $data, so the data is sent to the browser, and I encourage you to send much larger blocks (32kB).
John W. Holmes wrote:
Is it possible to let a user download a file from your website but ifhecancel'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 filesizestatisticsand i need to know if a transfer is succeeded or aborted. It would even be nice if i could record the downloade filesize of thepartthe 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...