FTP via Proxy with cURL

Avatar von Elger Thiele

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();

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

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

$result = curl_exec($curl);
curl_close($curl);

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

Kommentare

Eine Antwort zu „FTP via Proxy with cURL“

  1. OK good stuff and how do I FTP a file from one place to another one?

Schreibe einen Kommentar

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


Für das Handling unseres Newsletters nutzen wir den Dienst HubSpot. Mehr Informationen, insbesondere auch zu Deinem Widerrufsrecht, kannst Du jederzeit unserer Datenschutzerklärung entnehmen.