download a file from ftp using curl and php

前端 未结 6 1637
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 03:32

I\'m trying to download a file from an ftp server using curl and php but I can\'t find any documentation to help

$curl = curl_init();
curl_setopt($curl, CURL         


        
6条回答
  •  长情又很酷
    2020-11-30 03:42

    My guess is that your URL is pointing towards a directory, not a file. You would need to feed CURLOPT_URL the full URL to the file. Also if you want to download a file you might want to save it somewhere.

    Working example:

    $curl = curl_init();
    $file = fopen("ls-lR.gz", 'w');
    curl_setopt($curl, CURLOPT_URL, "ftp://ftp.sunet.se/ls-lR.gz"); #input
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FILE, $file); #output
    curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
    curl_exec($curl);
    curl_close($curl);
    fclose($file);
    

提交回复
热议问题