Download script by curl method

3

I'm having some problems with the script process below.

Literally it is downloading very slowly, and this is when the download process does not fall or restart from scratch.

Is there any way to fix this, so that the download is done normally, with a more effective process?

Downloadable files are up to 350MB .

Update:

Someone could tell me what problem I've already changed in php.ini with the settings of memory_limit , post_max_size , upload_max_filesize and max_execution_time . slow download process.

<?php
$file = 'http://thumb.mais.uol.com.br/15540367.mp4';
download($file,314572800);


function download($file,$chunks){
    set_time_limit(0);
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-disposition: attachment; filename='.basename($file));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    header('Pragma: public');
    $size = get_size($file);
    header('Content-Length: '.$size);

    $i = 0;
    while($i<=$size){

    get_chunk($file,(($i==0)?$i:$i+1),((($i+$chunks)>$size)?$size:$i+$chunks));
        $i = ($i+$chunks);
    }

}

function chunk($ch, $str) {
    print($str);
    return strlen($str);
}

function get_chunk($file,$start,$end){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $file);
    curl_setopt($ch, CURLOPT_RANGE, $start.'-'.$end);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'chunk');
    $result = curl_exec($ch);
    curl_close($ch);
}

function get_size($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    return intval($size);
}
?>
    
asked by anonymous 12.09.2015 / 07:38

1 answer

1
    //Tempo de execução ilimitado, visto que você
    //baixará arquivos grandes
    set_time_limit(0);     
    /*Ponteiro do Curl*/
    $ch = curl_init();

    /*Ponteiro do arquivo que será salvo*/
    $fp = fopen($destino, "w");

    /*Configuração*/
    $options = [
                 CURLOPT_RETURNTRANSFER => true, //preciso da resposta armazenada
                 CURLOPT_TIMEOUT => 120,         // limite de 120 segundos
                 CURLOPT_URL => $url,            //recurso a ser procurado
                 CURLOPT_FILE => $fp,            //ponteiro do arquivo
                 CURLOPT_HEADER => false,        //Para não corromper o arquivo
               ];

    /*Aplica a configuração*/
    curl_setopt_array($ch, $options);

    /*Baixa o arquivo*/
    curl_exec($ch);

    /*Fecha o ponteiro do arquivo*/
    fclose($fp);

    /*Fecha o Curl*/
    curl_close($ch);
    
19.09.2015 / 23:05