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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$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);
$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);
$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);

15 Minuten knallharter Fokus!

Fokus-Webinar: Warum fehlende Daten der #1-Killer für Deine AI-Projekte sind.

Der #1-Killer für Deine AI-Projekte!

Nimm Dir 15 Minuten Zeit und wir erklären dir:

  • Warum fehlende Daten der eigentliche Killer für Deine AI-Projekte sind.
  • Was Dein Ausweg auf der Silo-Falle ist.
  • Konkret: Lösungsansätze aus der bei uns gelebten Praxis!
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.