Hello, if you need to check the completed downloads from your website. you can use the following code $download_rate = 500; //Download rate 500Kb/sec $downloaded_bytes=0; header('Cache-control: private'); header('Content-Type: application/octet-stream'); header('Content-Length: '.filesize($file)); header('Content-Disposition: filename='.$filename); flush(); $file = fopen($file, "r"); while(!feof($file)) { // send the current file part to the browser $buf = fread($file, round($download_rate * 1024)); print $buf; $downloaded_bytes += strlen($buf); // flush the content to the browser flush(); // sleep one second sleep(1); } fclose($file); if($downloaded_bytes==filesize($file)) { //Do any thing like update your database or any thing else } Code (markup):
sure you can. you can do this by inserting every successful download into the database to make your report about the count of successful downloads. example: $download_rate = 500; //Download rate 500Kb/sec $downloaded_bytes=0; header('Cache-control: private'); header('Content-Type: application/octet-stream'); header('Content-Length: '.filesize($file)); header('Content-Disposition: filename='.$filename); flush(); $file = fopen($file, "r"); while(!feof($file)) { // send the current file part to the browser $buf = fread($file, round($download_rate * 1024)); print $buf; $downloaded_bytes += strlen($buf); // flush the content to the browser flush(); // sleep one second sleep(1); } fclose($file); if($downloaded_bytes==filesize($file)) { /* Here you can insert into the database the downloaded size ($downloaded_bytes) of each file and each one row means one successful download NOTE: you may need to connect again to database before insert query as the database connection may lost for a large file as it takes more time to download so make sure to open connection before insert query. */ } /* Then you can make another file to run report by selecting the count of rows to get the number of successful downloads */ Code (markup):
This accomplishes not a whole lot, to be fair. This sends the entire file to the browser, but it doesn't guarantee the user received all of it, or even clicked cancel before receiving everything. The additional check at the bottom is pointless, because at the end of the loop, the whole file will have been sent. PHP won't exit the loop in the middle or jump right out of it. You're probably better off checking connection_status() inside the loop, but that's not entirely fail-safe either. On a side note, I would calculate the download rate outside the loop. You're literally calling rand() and doing math thousands of times during a download.
Thanks for your comment. but the example before not to count the downloaded bytes even if the download not completed. this code just tell you when the download is completed but if not cannot tell you the downloaded bytes. if you have another code discussing your idea please post it