Skip to content

Instantly share code, notes, and snippets.

@omidnasri
Forked from fordnox/check-ftp.php
Created April 12, 2019 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omidnasri/8782396efb518d258ff4189ddccdb7d1 to your computer and use it in GitHub Desktop.
Save omidnasri/8782396efb518d258ff4189ddccdb7d1 to your computer and use it in GitHub Desktop.
Check ftp connection with PHP
<?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