来自远程服务器的 PHP 目录列表

发布于 2024-08-10 18:31:56 字数 45 浏览 2 评论 0原文

如何使用 opendir 函数读取知道其 IP 地址的远程服务器的目录列表?

How can i use opendir function to read the directory list of a remote server knowing its IP address?

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

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

发布评论

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

评论(3

不打扰别人 2024-08-17 18:31:56

假设您尝试使用 HTTP 访问远程服务器,您将无法直接执行此操作。由于 HTTP 实际上是要访问远程服务器的 Web 服务,因此它控制如何向您呈现文件。对于许多 Web 服务器,您可以打开文件夹内容的“索引”。这会导致文件夹中的条目以 HTML 形式显示在浏览器中。为了遍历目录结构,您需要解析此 HTML 以查找路径信息。

如果您使用 FTP,从 5.0 版开始,您可以将 ftp://... URL 传递给 opendir()。请注意,您的服务器管理员可以关闭此功能。如果您无法直接使用此功能,请参阅 PHP 的FTP 功能手册(包括ftp_nlist() 用于列出文件)。

上述参考文献中的一个示例:

<?php

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");

// output $contents
var_dump($contents);

?>

Assuming you are trying to access the remote server using HTTP, you won't be able to do this directly. Since HTTP is really going access the web service of your remote server, it controls how the files are presented to you. In the case of many web servers, you can turn on "indexes" for folder contents. This causes the entries in the folder to be displayed in your browser as HTML. In order to traverse the directory structure, you would need to parse this HTML to find the path information.

If you are using FTP, you can pass the ftp://... URL to opendir() as of version 5.0. Note that this capability can be turned off by your server admin. If you cannot use this feature directly, see the FTP functions manual for PHP (including ftp_nlist() for listing files).

An example from the above references:

<?php

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");

// output $contents
var_dump($contents);

?>
唐婉 2024-08-17 18:31:56

对于公众可见的文件/文件夹,您可以使用以下脚本来完成:No FTP, no strugglin。它将返回远程文件夹中的所有文件名。如果可以用浏览器打开该文件夹,也可以用php读取。快乐的水蛭;)

更新:这仅列出了可供公众使用的文件,当然不是仅对特定用户可见的文件!

function get_text($filename) {

    $fp_load = fopen("$filename", "rb");

    if ( $fp_load ) {

            while ( !feof($fp_load) ) {
                $content .= fgets($fp_load, 8192);
            }

            fclose($fp_load);

            return $content;

    }
}


$matches = array();

preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", get_text('http://www.xxxxx.com/my/cool/remote/dir'), $matches);

foreach($matches[2] as $match) {
    echo $match . '<br>';
}

For files/folders that are visible to the public you can do it with this script: No FTP, no strugglin. It will give you back all filenames in a remote folder. If you can open the folder with a browser, you can also read it with php. Happy leeching ;)

UPDATE: This only lists the files that are available for the public, for sure not files that are only visible for a certain user!

function get_text($filename) {

    $fp_load = fopen("$filename", "rb");

    if ( $fp_load ) {

            while ( !feof($fp_load) ) {
                $content .= fgets($fp_load, 8192);
            }

            fclose($fp_load);

            return $content;

    }
}


$matches = array();

preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", get_text('http://www.xxxxx.com/my/cool/remote/dir'), $matches);

foreach($matches[2] as $match) {
    echo $match . '<br>';
}
欢烬 2024-08-17 18:31:56

假设您的服务器上加载了 php 的 ftp 扩展,您应该能够使用 phps ftp 函数

Assuming you have the ftp extension loaded for php on your server, you should be able to use phps ftp functions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文