Download file from URL using CURL

52,306

Solution 1

Give this a go

<?php

    $output_filename = "testfile.igc";

    $host = "http://www.xcontest.org/track.php?t=2avxjsv1.igc";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $host);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, false);
    curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $result = curl_exec($ch);
    curl_close($ch);

    print_r($result); // prints the contents of the collected file before writing..


    // the following lines write the contents to a file in the same directory (provided permissions etc)
    $fp = fopen($output_filename, 'w');
    fwrite($fp, $result);
    fclose($fp);
?>
#

or if you want to put it within a loop for parsing several links... you need some functions.. here is a rough idea....

<?php

    function collect_file($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, false);
        curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $result = curl_exec($ch);
        curl_close($ch);
        return($result);
    }

    function write_to_file($text,$new_filename){
        $fp = fopen($new_filename, 'w');
        fwrite($fp, $text);
        fclose($fp);
    }


    // start loop here

    $new_file_name = "testfile.igc";
    $url = "http://www.xcontest.org/track.php?t=2avxjsv1.igc";

    $temp_file_contents = collect_file($url);
    write_to_file($temp_file_contents,$new_file_name)

    // end loop here
?>

Solution 2

@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

Share:
52,306
user1789813
Author by

user1789813

Updated on November 16, 2020

Comments

  • user1789813
    user1789813 over 3 years

    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 looks like the following, but it produces empty download files only:

    $DLFile= "testfile.igc";
    $DLURL="http://www.xcontest.org/track.php?t=2avxjsv1.igc"; 
    $fp = fopen ($DLFile, 'w+');
    $ch = curl_init($DLURL);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    

    An other strange thing is when entering the URL in the web browser I don't get the file. It can I could only download the file when clicking the link on the web site!.

    Any advice is very appreciated!

  • user1789813
    user1789813 over 11 years
    Thanks a lot that is the solution!
  • Chris
    Chris over 11 years
    The functions I just added at the bottom will help if you are wanting to loop and create several files.. :)
  • Chris
    Chris over 11 years
    curl_setopt($ch, CURLOPT_REFERER, "xcontest.org"); was what solved it really.
  • chocolata
    chocolata almost 8 years
    Works like a charm!
  • Muntashir Akon
    Muntashir Akon over 7 years
    @TheBumpaster The URL you have used may redirect to another page. Set CURLOPT_FOLLOWLOCATION to true to solve this.
  • Maris B.
    Maris B. almost 3 years
    Use a built-in file_put_contents instead of the custom write_to_file function