PHP - 另一台服务器上的 opendir

2022-01-01 00:00:00 directory php opendir

我对 PHP 有点陌生.

I'm kinda new to PHP.

我有两个不同的主机,我希望其中一个中的 php 页面向我显示另一个的目录列表.我知道如何在同一主机上使用 opendir() 但是否可以使用它来访问另一台机器?

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?

提前致谢

推荐答案

您可以使用 PHP 的 FTP Capabilities 远程连接到服务器并获取目录列表:

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

相关文章