I’ve written some code that accesses an FTP server and pulls down flat files. It works on my development environment in WAMP (Windows; 5.3.10) but on the production server (5.3.2 – Linux) it fails when calling ftp_nlist.
It sits there for about 30 seconds and then dies with the coded error message [FTP] Unable to retrieve files in the directory.
Here is the relevant code:
$link = ftp_connect(constant("FTP_SERVER"));
if(!$link)
die("Unable to open connection to FTP Server: " + constant("FTP_SERVER"));
if(!ftp_login($link,
constant("FTP_USER"),
constant("FTP_PASSWORD")))
die("Unable to login; check username and password.");
if(!ftp_chdir($link,
constant("FTP_PATH")))
die("Unable to change to directory: " + constant("FTP_PATH"));
ftp_pasv($link, true);
clearDownloadDir(); // Delete already downloaded files
/* Get a list of all files */
$files = ftp_nlist($link, '.');
if(!$files)
die("[FTP] Unable to retrieve files in the directory.");
The constants are defined in another file as:
define("FTP_SERVER", "ftp.{server}");
define("FTP_PATH", "./Australia/");
define("FTP_USER", "{user}");
define("FTP_PASSWORD", "{password}");
The thing that gets me is that it is connecting (evidenced by the fact its not dying on ftp_connect) and works locally. FTP_NLIST is returning boolean TRUE (1). Any ideas?
It turns out the firewall on the shared server this code was running on was preventing FTP entering passive mode. Very odd.