tags:

views:

173

answers:

3

Hey guys, I have been trying to limit the bandwidth with PHP.

Can you please help here? I can't get the download rate to be limited with PHP.

Thanks a million!

    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'); 
A: 

I would limit the bandwidth via the server, e.g. IIS or Apache.

Nik
If I needed to do it, I'd do it on the operating system or network - but in god's name....why? I'd agree it makes sense for testing, but in production the bandwidth slows down **your** server as well as the client.
symcbean
A: 

CURLINFO_SPEED_DOWNLOAD informs you of the download speed; it's not an option you can set. That said, if it were an option, you'd be setting it in the wrong place (in the part where you make a HEAD request to obtain the file size, – which by the way, is unnecessary, but that's irrelevant here – and not where you actually download the file).

You can do it with PHP streams, where you would loop and either retrieve/send more data or wait according to your limit, but I don't think there's a way to exchange a curl resource for a PHP stream. Your only alternative may be using the http wrapper instead.

You could also try CURLOPT_FILE and save the file to a "php://temp" stream, and then read from it, but I'm not sure it would work.

Artefacto
+3  A: 

CURLOPT_MAX_RECV_SPEED_LARGE is the option you want

Daniel Stenberg
http://bugs.php.net/bug.php?id=51815 It was added just recently and is not present in any production releases. But still correct answer, thanks.
dev-null-dweller