tags:

views:

18

answers:

2

hey guys, i love working with mamp ( i have the pro version). i'm currently trying to connect to a ftp server and list the files up there. however the connection is successful but it won't list the files.

$contents = ftp_nlist($conn_id, $path);

returns bool(false)

however the script is working if i run it on my real webserver. is there maybe some preference i have to set to get it working locally as well?

regards

A: 

I have MAMP so I did a little testing using the first example in the php manual page for ftp_nlist, and was getting the same error as you (bool(false)). Turns out, if using that code:

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");

// output $contents
var_dump($contents);

and the login fails either because of a bad username or password, if will fail with the bool(false) message.

Better to do something that will give you better info as to where it failed (if it does fail):

// set up basic connection variables
$ftp_server='127.0.0.1';
$ftp_user_name='user';
$ftp_user_pass='pass';

//initiate connection
$conn_id = ftp_connect($ftp_server);


// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die ('login failed');

// get contents of the current directory
$contents = ftp_nlist($conn_id, ".") or die ('could not get contents');

// output $contents
var_dump($contents);

I tested this and it works. Given the right credentials, it will log in and dump the dir. If the credentials are bad, it will die with a 'login failed' message. If the path is bad, it will die with a 'could not get contents' message; so at least if it does break down you'll have a better idea of where.

HTH

stormdrain
A: 

ftp_nlist() can sometimes return false if you're behind a firewall. Try turning on passive mode with ftp_pasv() directly after authenticating, like so:

$conn_id = ftp_connect($server_ip);
$login_result = ftp_login($conn_id, $username, $password);
ftp_pasv($conn_id, true);
$contents = ftp_nlist($conn_id, $path);
skoh-fley