Test FTP connection with PHP

后端 未结 6 1040
独厮守ぢ
独厮守ぢ 2021-02-05 08:55

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

6条回答
  •  忘掉有多难
    2021-02-05 09:26

    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 :)

提交回复
热议问题