我已经编写了一个脚本,使您能够以我允许的最大文件速度下载文件,但是当我允许像 10000kB/s 这样的“无限”速度时,ftell 工作起来很奇怪,它的行为就像它以 10000kBps 的速度下载一样,这不是真的,我无法在数据库中进行计算,如剩余时间、当前下载速度等......
所以浏览器会在一段时间后下载文件,但在数据库中它已经像“下载”一样,即使我设置了无限速度,我怎么能进行一些精确计算,以便用户可以以网络速度下载文件,数据库值也是以他的网速计算而不是由ftell();
哪个取决于$download_rate;
...?
提前致谢!
<?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);
///////HERE THE CALCULATIONS ARE TOTALLY WRONG, BECAUSE IT ALL DEPENDS ON THE INPUT OF $download_rate;
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);
}
?>