PHP Download file, limit max speed and calculate downloading speed
I have written a script that gives you ability to download the file with
my maximum file speed that I allow, however when I allow 'unlimited' speed
like 10000kB/s then the ftell works strange, it behaves like it downloads
with 10000kBps speed, which is not true and I can not make calculations in
database like time remaining, current download speed and so on... So
browser downloads file after some time, but in database it is already like
'downloaded', how could I make some precision calculations even I set the
unlimited speed so user can download a file at the speed of the network
and the database values are also counted by his network speed not by the
ftell()...?
Thanks in advance!
<?php
while(!feof($fopen)) {
//echo fread($fopen, 4096);
$this->get_allowed_speed_limit($download_rate);
//$download_rate = 350;
print fread($fopen, round($download_rate * 1024));
sleep(1); //needed for download speed limit
if(connection_status() != 0 || connection_aborted()) {
$bytes_transferred = ftell($fopen);
if($bytes_transferred < $bytes) {
//CANCELLED
$this->download_unsuccessfull($file_name);
} else {
//CANCELLED (but gets executed only on strange networks like
eduroam in CZE)
$this->download_unsuccessfull($file_name);}
flush();
die;
} else {
$progress = ftell($fopen) / $bytes * 100;
if($progress >= 100) {
//DONE
$this->download_successfull($file_name);
flush();
} else {
//DOWNLOADING
if(ftell($fopen) != 0) {
$bytes_transferred = ftell($fopen);
$time_end = microtime(true);
$time = $time_end - $time_start;
$dl_speed = floor(($bytes_transferred / $time) / 1000);
mysqli_query($con, "UPDATE `download_meter` SET
`current_speed` = '".mysqli_real_escape_string($con,
$bytes_transferred)."'");
$this->update_active_downloads($file_name,
$bytes_transferred, $dl_speed);
}
flush();
}
}
//Activate this for delay download.
//flush();
//sleep(1);
}
?>
No comments:
Post a Comment