PHP - 另一台服务器上的opendir

Aux*_*ary 1 php directory opendir

我是PHP的新手.

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

提前致谢

And*_*ore 6

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

// 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);
Run Code Online (Sandbox Code Playgroud)


Mar*_*cel 6

尝试:

<?php

$dir = opendir('ftp://user:pass@domain.tld/path/to/dir/');

while (($file = readdir($dir)) !== false) {
    if ($file[0] != ".") $str .= "\t<li>$file</li>\n";
}

closedir($dir);

echo "<ul>\n$str</ul>";
Run Code Online (Sandbox Code Playgroud)