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, CURLOPT_URL,"ftp://$_FTP[server]"); curl_setopt($curl, CURLOPT_FTPLISTONLY, 1); curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec ($curl);
i can get a list of files but thats about it
cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains.
To download a file, drag the file from the browser window to the desktop. You can also double-click the filename, and you will be prompted to either save or open the file. To upload a file, drag the file from your hard drive to the browser window.
To upload to an FTP server, you specify the entire target file path and name in the URL, and you specify the local file name to upload with -T, --upload-file . Optionally, you end the target URL with a slash and then the file component from the local path will be appended by curl and used as the remote file name.
curl is the goto tool for anything HTTP related but you can also use it for your FTP and FTPS tasks.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With