Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting Bandwidth of Download with cURL

Tags:

php

curl

download

I have been trying to limit the bandwidth with PHP. I can't get the download rate to be limited with PHP.

Can you please help here?

function total_filesize($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "$url");
    curl_setopt($ch, CURLINFO_SPEED_DOWNLOAD,12); //ITS NOT WORKING! 
    curl_setopt($ch, CURLOPT_USERAGENT,
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) ".
            "Gecko/20071127 Firefox/2.0.0.11");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_NOBODY, true);


    $chStore = curl_exec($ch);
    $chError = curl_error($ch);
    $chInfo = curl_getinfo($ch);
    curl_close($ch);
    return $size = $chInfo['download_content_length'];
}

function __define_url($url) {
    $basename = basename($url);
    Define('filename',$basename);
    $define_file_size = total_filesize($url);
    Define('filesizes',$define_file_size);
}

function _download_file($url_file) { 
    __define_url($url_file);

    // $range = "50000-60000";
    $filesize = filesizes;
    $file = filename; 
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.$file.'"'); 
    header('Content-Transfer-Encoding: binary');
    header("Content-Length: $filesize");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"$url_file");
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    //  curl_setopt($ch, CURLOPT_RANGE,$range);

    curl_exec($ch);
    curl_close($ch);
}
_download_file('http://rarlabs.com/rar/wrar393.exe'); 

like image 520
Saxtor Avatar asked Jun 18 '10 01:06

Saxtor


People also ask

How to limit download speed in wget?

When using wget, you can limit the file retrieval rate with the --limit-rate switch. The value can be expressed in bytes, kilobytes with the k suffix, or megabytes with the m suffix. The following examples show how to limit the file download speed to 50KB/s with wget command. To turn off its output, use the -q flag.

What is limit download speed?

The download speed limit is an option that allows you to control the content delivery speed to end-users.


1 Answers

CURLOPT_MAX_RECV_SPEED_LARGE is the option you want.

Added in curl 7.15.5. Present in PHP/CURL since PHP 5.4.0

like image 76
Daniel Stenberg Avatar answered Sep 23 '22 06:09

Daniel Stenberg