Download file from URL using CURL

后端 未结 3 1216
一整个雨季
一整个雨季 2020-12-16 02:28

I try to download file using a php-script from an URL like the following:

http://www.xcontest.org/track.php?t=2avxjsv1.igc

The code I use l

3条回答
  •  星月不相逢
    2020-12-16 02:58

    @Chris' answer works, but this seems to work better to download very large files without running out of memory, since it doesn't download the whole file into a variable before writing to disk:

    $file_url = 'http://www.test.com/images/avatar.png';
    $destination_path = "downloads/avatar.png";
    
    $fp = fopen($destination_path, "w+");
    
    $ch = curl_init($file_url);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_exec($ch);
    $st_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    fclose($fp);
    
    if($st_code == 200)
     echo 'File downloaded successfully!';
    else
     echo 'Error downloading file!';
    

    Source: https://www.kodingmadesimple.com/2018/02/php-download-file-from-url-curl.html

提交回复
热议问题