Skip to content

Instantly share code, notes, and snippets.

@fordnox
Created April 5, 2017 13:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fordnox/bf1f307f0aab2d8b9f9e0d334af5dd68 to your computer and use it in GitHub Desktop.
Save fordnox/bf1f307f0aab2d8b9f9e0d334af5dd68 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