Download File to server from URL

后端 未结 12 1221
旧巷少年郎
旧巷少年郎 2020-11-22 04:05

Well, this one seems quite simple, and it is. All you have to do to download a file to your server is:

file_put_contents(\"Tmpfile.zip\", file_get_contents(\         


        
12条回答
  •  自闭症患者
    2020-11-22 04:35

    prodigitalson's answer didn't work for me. I got missing fopen in CURLOPT_FILE more details.

    This worked for me, including local urls:

    function downloadUrlToFile($url, $outFileName)
    {   
        if(is_file($url)) {
            copy($url, $outFileName); 
        } else {
            $options = array(
              CURLOPT_FILE    => fopen($outFileName, 'w'),
              CURLOPT_TIMEOUT =>  28800, // set this to 8 hours so we dont timeout on big files
              CURLOPT_URL     => $url
            );
    
            $ch = curl_init();
            curl_setopt_array($ch, $options);
            curl_exec($ch);
            curl_close($ch);
        }
    }
    

提交回复
热议问题