-
-
Save dexit/af083c6ccf47c628ad565ff7af7120f1 to your computer and use it in GitHub Desktop.
Check ftp connection with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$host = 'example.com'; | |
$password = 'pass'; | |
$username = 'username'; | |
try { | |
$result = checkFtp($host, $username, $password); | |
} catch(Exception $e) { | |
$result = $e->getMessage(); | |
} | |
if($result) { | |
print 'ok'; | |
} else { | |
print 'fail'; | |
} | |
function checkFtp($host, $username, $password, $port = 21, $timeout = 10) { | |
$con = ftp_connect($host, $port, $timeout); | |
if (false === $con) { | |
throw new Exception('Unable to connect to FTP Server.'); | |
} | |
$loggedIn = ftp_login($con, $username, $password); | |
ftp_close($con); | |
if (true === $loggedIn) { | |
return true; | |
} else { | |
throw new Exception('Unable to log in.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment