Downloading big files and writing it locally

前端 未结 2 2034
春和景丽
春和景丽 2021-02-11 07:31

which is the best way to download from php large files without consuming all server\'s memory?

I could do this (bad code):

$url=\'http://server/bigfile\'         


        
2条回答
  •  情深已故
    2021-02-11 08:27

    You can use curl and the option CURLOPT_FILE to save the downloaded content directly to a file.

    set_time_limit(0);
    $fp = fopen ('file', 'w+b');
    $ch = curl_init('http://remote_url/file');
    curl_setopt($ch, CURLOPT_TIMEOUT, 75);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    

提交回复
热议问题