PHP - 在另一台服务器上的 opendir

发布于 2024-10-21 21:41:51 字数 125 浏览 5 评论 0原文

我对 PHP 有点陌生。

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

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

椒妓 2024-10-28 21:41:51

尝试:

<?php

$dir = opendir('ftp://user:[email protected]/path/to/dir/');

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

closedir($dir);

echo "<ul>\n$str</ul>";

Try:

<?php

$dir = opendir('ftp://user:[email protected]/path/to/dir/');

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

closedir($dir);

echo "<ul>\n$str</ul>";
﹏半生如梦愿梦如真 2024-10-28 21:41:51

您可以使用 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);

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);
许你一世情深 2024-10-28 21:41:51

我无法让 FTP 建议发挥作用,所以我采取了一种更非常规的路线,基本上是从“索引”页面中提取 html 并提取文件名。

索引页:

/files 的索引

  • 父目录
  • 1.jpg
  • 2.jpg
  • 提取码:

        $dir = "http://www.yoursite.com/files/";
        $contents = file_get_contents($dir);
        $lines = explode("\n", $contents);
        foreach($lines as $line) {
            if($line[1] == "l") { // matches the <li> tag and skips 'Parent Directory'
                $line = preg_replace('/<[^<]+?>/', '', $line); // removes tags, curtousy of http://stackoverflow.com/users/154877/marcel
                echo trim($line) . "\n";
            }
        }
    

    I was unable to get the FTP suggestions to work so I took a more unconventional route, basically it yanks the html from the "Index of" page and extracts the filenames.

    Index page:

    Index of /files

  • Parent Directory
  • 1.jpg
  • 2.jpg
  • Extraction code:

        $dir = "http://www.yoursite.com/files/";
        $contents = file_get_contents($dir);
        $lines = explode("\n", $contents);
        foreach($lines as $line) {
            if($line[1] == "l") { // matches the <li> tag and skips 'Parent Directory'
                $line = preg_replace('/<[^<]+?>/', '', $line); // removes tags, curtousy of http://stackoverflow.com/users/154877/marcel
                echo trim($line) . "\n";
            }
        }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文