Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

download a file from ftp using curl and php

Tags:

php

curl

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

like image 719
Ryan Avatar asked Jul 24 '09 15:07

Ryan


People also ask

Can I use cURL in PHP?

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.

How do I download files from an FTP site?

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.

How do I upload to FTP using cURL?

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.

Can you cURL FTP?

curl is the goto tool for anything HTTP related but you can also use it for your FTP and FTPS tasks.


1 Answers

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); 
like image 150
4 revs, 4 users 77% Avatar answered Oct 13 '22 20:10

4 revs, 4 users 77%