Limit download speed using PHP

前端 未结 6 2373
北荒
北荒 2020-12-02 13:37

I found on Google some PHP scripts to limit the download speed of a file, but the file download at 10 Mbps or if it download at 80 kbps as i set it, after 5 mb, it stops dow

6条回答
  •  囚心锁ツ
    2020-12-02 14:33

    First of all max_execution_time is the execution time of your script. Sleeping is not part of it.

    Regarding speed limiting you could use something like a Token bucket. I've put everything into one convenient library for you: bandwidth-throttle/bandwidth-throttle

    use bandwidthThrottle\BandwidthThrottle;
    
    $in  = fopen(__DIR__ . "/resources/video.mpg", "r");
    $out = fopen("php://output", "w");
    
    $throttle = new BandwidthThrottle();
    $throttle->setRate(100, BandwidthThrottle::KIBIBYTES); // Set limit to 100KiB/s
    $throttle->throttle($out);
    
    stream_copy_to_stream($in, $out);
    

提交回复
热议问题