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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1688564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:31:28  来源:igfitidea点击:

PHP directory list from remote server

phpdirectory

提问by mck89

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

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

回答by jheddings

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.

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

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 functionsmanual for PHP (including ftp_nlist()for listing files).

如果您使用的是 FTP,则可以将ftp://...URL传递到opendir()版本 5.0。请注意,您的服务器管理员可以关闭此功能。如果您不能直接使用此功能,请参阅PHP的FTP 函数手册(包括ftp_nlist()列表文件)。

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

?>

回答by Sliq

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

对于对公众可见的文件/文件夹,您可以使用以下脚本执行此操作:没有 FTP,没有挣扎。它将返回远程文件夹中的所有文件名。如果可以用浏览器打开文件夹,也可以用php阅读。快乐偷窃 ;)

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>';
}

回答by Toby Allen

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

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