How to download file from link use Curl PHP

Discussion in 'PHP' started by startdream, Sep 26, 2014.

Thread Status:
Not open for further replies.
  1. #1
    closed
     
    Solved! View solution.
    Last edited: Sep 26, 2014
    startdream, Sep 26, 2014 IP
  2. #2
    Hi, try this:

    $source ="https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=1152872617&weight=1.35";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $source);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSLVERSION,3);
    $data = curl_exec ($ch);
    $error = curl_error($ch);
    curl_close ($ch);

    $destination ="./files/test.pdf";
    $file = fopen($destination,"w+");
    fputs($file, $data);
    fclose($file);
     
    Argento, Sep 30, 2014 IP
  3. startdream

    startdream Member

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    28
    #3
    Hi Argento,

    Thank for you idea,

    How i can get type of file? Because i don't know type of file in dowload link. It can be zip, zar....
     
    startdream, Oct 2, 2014 IP
  4. Argento

    Argento Active Member

    Messages:
    69
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    53
    #4
    Hi ! You can do it in this way:

    $filename="this.is.the.file.png";
    $filename_arr = explode(".", $filename);
    $count_of_elements = count($filename_arr);
    $file_extension = $filename_arr[$count_of_elements -1];
     
    Argento, Oct 6, 2014 IP
  5. startdream

    startdream Member

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    28
    #5
    Hi Argento,

    My question is: How to get file type of file after statement: $data = curl_exec ($ch); ?
     
    startdream, Oct 6, 2014 IP
  6. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #6
    $extension = pathinfo($YOURFILENAME, PATHINFO_EXTENSION);
     
    EricBruggema, Oct 6, 2014 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    A couple of things.

    With CURLOPT_RETURNTRANSFER alone, you're loading the entire file into the memory, which may lead to problems. It's usually better if you tell cURL to write directly to a fopen() resource using CURLOPT_FILE.

    http://php.net/manual/en/function.curl-setopt.php#99082

    And, you may not be able to get the file extension from the URL (like in the URL above). But you can usually get it from the HTTP headers. Don't rely on it, though.
     
    nico_swd, Oct 11, 2014 IP
Thread Status:
Not open for further replies.