FTP via Proxy with cURL

There should be no problem to download files dynamically via FTP and user/password authentication. But what if using an http proxy in between?
Well, there are some useful options you may set with curl_set_option but in our case most were not accepted.
The main problem was getting the list of files laying on the ftp server. Ok, lets take a look at the solution.
First of all you have to use a single link for the ftp connection or an authentication through http tunnel will fail, like: ftp://user:pwd@ftp.host:port/
After that the proxy has to be set: http://proxy:port (alternatively to the port the curlopt proxyport may be used)
These are the only mandatory options to receive data.

One more thing to mention is that if no file for downloading is given (e.g. ftp.server.com/file_to_download) the current folder will be parsed. Looking at the received data you will maybe a bit confused, because the data format is html. CUROPT_FTPLISTONLY for example won’t work, either.

Here a small example:

$curl = curl_init();

curl_setopt($curl, CURLOPT_PROXY, “proxy.server.com”);
curl_setopt($curl, CURLOPT_PROXYPORT, “80”);

curl_setopt($curl, CURLOPT_URL, “ftp://user:pwd@ftp.server.com:21”);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl);

//get filenames from html
preg_match_all("!< A.*? HREF=\"([^\"]*)\"[^>]*>(.*?)</A>!", $result, $return);

Ein Kommentar

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert