MAMP: local-environment … connecting to ftp won't work?
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 wo开发者_运维问答n'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
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
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);
I found an issue that resolves this. If you boot your Mac into 32 bit mode, it appears to be working. To boot your into 32 bit mode, hold down the 3 and the 2 on the keyboard when booting. I am able to get this function to work.
RESOLUTION: I found the resolution for this.
To solve do the following: add this to the php script: var_dump(getenv(TMPDIR)); exit;
Mine returned a strange tmp dir location or /var/folders
Then I did: sudo chmod -R 0777 /var/folders
Now it works as expected!
精彩评论