Test FTP connection with PHP

后端 未结 6 930
独厮守ぢ
独厮守ぢ 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();
    }
    
    0 讨论(0)
  • 2021-02-05 09:14

    Hey. I'm new here so maybe posting this late answer is not welcome, but it may help people in the future.

    The reason why it's not working with MediaTemple is because they only accept passive connections.

    After logging in with ftp_login(), simply call ftp_pasv($ftp, TRUE); and you'll be set.

    0 讨论(0)
  • 2021-02-05 09:21

    Simply do a check if ftp_nlist() is an array.

    Like:

    echo is_array(ftp_nlist($con, ".")) ? 'Connected!' : 'not Connected! :(';

    References:

    • http://php.net/manual/en/function.is-array.php
    • http://php.net/manual/en/function.ftp-nlist.php
    0 讨论(0)
  • 2021-02-05 09:24

    Note that you're already dieing when you fail to connect, so you can assume that you are connected. However, you can also check the status of the connection using:

    echo $con !== FALSE ? 'Connected' : "Couldn't connect";
    

    ftp_connect: Returns a FTP stream on success or FALSE on error.

    0 讨论(0)
  • 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 :)

    0 讨论(0)
  • 2021-02-05 09:29

    Hello I have tried this..Working properly.

    set_time_limit(300);//for setting 
    $path='/'.date('dmY').'';
    $ftp_server='';
    $ftp_server_port="";
    $ftp_user_name='';
    $ftp_user_pass="";
    
    // set up a connection to ftp server
    $conn_id = ftp_connect($ftp_server, $ftp_server_port); 
    // login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    
    // check connection and login result
    if ((!$conn_id) || (!$login_result)) { 
        echo "Fail</br>";
    } else {
        echo "Success</br>";
        // enabling passive mode
        ftp_pasv( $conn_id, true );
        // get contents of the current directory
        $contents = ftp_nlist($conn_id, $path);
        // output $contents
        var_dump($contents);
    }
    
    // close the FTP connection
    ftp_close($conn_id);
    
    0 讨论(0)
提交回复
热议问题