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

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

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

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

推荐答案

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

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.

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

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

?>

相关文章