Test FTP connection with PHP

后端 未结 6 938
独厮守ぢ
独厮守ぢ 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:14

    Both ftp_connect() and ftp_login() return a boolean indicating success. Thus, something like this should do what you want, if I'm interpreting properly:

    try {
        $con = ftp_connect($server);
        if (false === $con) {
            throw new Exception('Unable to connect');
        }
    
        $loggedIn = ftp_login($con,  $username,  $password);
        if (true === $loggedIn) {
            echo 'Success!';
        } else {
            throw new Exception('Unable to log in');
        }
    
        print_r(ftp_nlist($con, "."));
        ftp_close($con);
    } catch (Exception $e) {
        echo "Failure: " . $e->getMessage();
    }
    

提交回复
热议问题