I am using the PHP script below to test FTP connections. Currently it is printing an array of the files, if it successfully connects.
How can I get it to also display
While I agree with the logic in the accepted answer from @Jakub of calling ftp_nlist() and testing data type with is_array(), this can be very slow and cumbersome with particularly large, bloated ftp directories like the ones I am currently working on. And I don't like the idea of creating a blank directory just for testing, which can be renamed/removed later as considered to be unneeded, perhaps by another developer or because you forgot what it was placed there for.
I am using a passive ftp connection, so for my purpose on cron scripts that can take a long time to execute and potentially require reconnection, I detect using this:
function check_connection_status($conn_id) {
return ftp_pasv($conn_id, true);
}
Issuing a fresh call to ftp_pasv() will make no alteration to the state of the ftp connection, but it will respond true if the connection is active and logged in/false if not so you can program to reconnect again :)