可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using the PHP script below to test FTP connections. Currently it is printing an array of the files, if it successfully connects.
How can I get it to also display a message, if it is able to connect? Like 'Connection Successful'.
$con = ftp_connect($server) or die("Couldn't connect");
ftp_login($con, $username, $password);
print_r(ftp_nlist($con, "."));
ftp_close($con);
EDIT
I have it working now but, I've tested this on a few domains I have on a MediaTemple server and they all seem be timing out. Yet, it works with all other domains I have tried. Are their servers blocking the request?
回答1:
Simply do a check if ftp_nlist()
is an array.
Like:
echo is_array(ftp_nlist($con, ".")) ? 'Connected!' : 'not Connected! :(';
References:
- http://php.net/manual/en/function.is-array.php
- http://php.net/manual/en/function.ftp-nlist.php
回答2:
Both ftp_connect() and ftp_login() return a boolean indicating success. Thus, something like this should do what you want, if I'm interpreting properly:
try {
$con = ftp_connect($server);
if (false === $con) {
throw new Exception('Unable to connect');
}
$loggedIn = ftp_login($con, $username, $password);
if (true === $loggedIn) {
echo 'Success!';
} else {
throw new Exception('Unable to log in');
}
print_r(ftp_nlist($con, "."));
ftp_close($con);
} catch (Exception $e) {
echo "Failure: " . $e->getMessage();
}
回答3:
Hey. I'm new here so maybe posting this late answer is not welcome, but it may help people in the future.
The reason why it's not working with MediaTemple is because they only accept passive connections.
After logging in with ftp_login()
, simply call ftp_pasv($ftp, TRUE);
and you'll be set.
回答4:
Hello I have tried this..Working properly.
set_time_limit(300);//for setting
$path='/'.date('dmY').'';
$ftp_server='';
$ftp_server_port="";
$ftp_user_name='';
$ftp_user_pass="";
// set up a connection to ftp server
$conn_id = ftp_connect($ftp_server, $ftp_server_port);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection and login result
if ((!$conn_id) || (!$login_result)) {
echo "Fail</br>";
} else {
echo "Success</br>";
// enabling passive mode
ftp_pasv( $conn_id, true );
// get contents of the current directory
$contents = ftp_nlist($conn_id, $path);
// output $contents
var_dump($contents);
}
// close the FTP connection
ftp_close($conn_id);
回答5:
While I agree with the logic in the accepted answer from @Jakub of calling ftp_nlist()
and testing data type with is_array()
, this can be very slow and cumbersome with particularly large, bloated ftp directories like the ones I am currently working on. And I don't like the idea of creating a blank directory just for testing, which can be renamed/removed later as considered to be unneeded, perhaps by another developer or because you forgot what it was placed there for.
I am using a passive ftp connection, so for my purpose on cron scripts that can take a long time to execute and potentially require reconnection, I detect using this:
function check_connection_status($conn_id) {
return ftp_pasv($conn_id, true);
}
Issuing a fresh call to ftp_pasv()
will make no alteration to the state of the ftp connection, but it will respond true if the connection is active and logged in/false if not so you can program to reconnect again :)
回答6:
Note that you're already die
ing when you fail to connect, so you can assume that you are connected. However, you can also check the status of the connection using:
echo $con !== FALSE ? 'Connected' : "Couldn't connect";
ftp_connect: Returns a FTP stream on success or FALSE on error.