What is the issue with the PHP code provided for checking the online/offline status of FTP servers?
The issue with the provided PHP code is that it does not properly handle errors that may occur when trying to connect to FTP servers. To solve this issue, we need to add error handling to catch any exceptions that may be thrown during the connection attempt.
<?php
function checkFTPStatus($server, $port, $timeout = 10) {
$ftp = ftp_connect($server, $port, $timeout);
if (!$ftp) {
return "Offline";
}
$login = ftp_login($ftp, 'anonymous', '');
if (!$login) {
ftp_close($ftp);
return "Offline";
}
ftp_close($ftp);
return "Online";
}
// Usage
$status = checkFTPStatus('ftp.example.com', 21);
echo $status;
?>