Search code examples
phpdirectoryopendir

PHP - opendir on another server


I'm kinda new to PHP.

I've got two different hosts and I want my php page in one of them to show me a directory listing of the other. I know how to work with opendir() on the same host but is it possible to use it to get access to another machine?

Thanks in advance


Solution

  • You could use PHP's FTP Capabilities to remotely connect to the server and get a directory listing:

    // set up basic connection
    $conn_id = ftp_connect('otherserver.example.com'); 
    
    // login with username and password
    $login_result = ftp_login($conn_id, 'username', 'password'); 
    
    // check connection
    if ((!$conn_id) || (!$login_result)) { 
        echo "FTP connection has failed!";
        exit; 
    }
    
    // upload the file
    $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 
    
    // check upload status
    if (!$upload) { 
        echo "FTP upload has failed!";
    } else {
        echo "Uploaded $source_file to $ftp_server as $destination_file";
    }
    
    // Retrieve directory listing
    $files = ftp_nlist($conn_id, '/remote_dir');
    
    // close the FTP stream 
    ftp_close($conn_id);