Created
April 5, 2017 13:17
-
-
Save fordnox/bf1f307f0aab2d8b9f9e0d334af5dd68 to your computer and use it in GitHub Desktop.
Check ftp connection with PHP
This file contains 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