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?
Example. $ftp_server = "ftp.example.com"; $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server"); $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$host= 'ftp.example.com'; $user = 'notarealusername'; $password = 'notarealpassword'; $ftpConn = ftp_connect($host); $login = ftp_login($ftpConn,$user,$password); // check connection if ((! $ftpConn) || (! $login)) { echo 'FTP connection has failed!
FTPConnect (Function) In french: FTPConnecte. Connects the current computer to an FTP server (File Transfer Protocol). The available secure connection modes are as follows: FTPS: FTP secured according to the SSL protocol with implicit encryption.
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();
}
Simply do a check if ftp_nlist()
is an array.
Like:
echo is_array(ftp_nlist($con, ".")) ? 'Connected!' : 'not Connected! :(';
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With