如何使用opendir函数读取知道其IP地址的远程服务器的目录列表?
答案 0 :(得分:11)
假设您尝试使用HTTP访问远程服务器,您将无法直接执行此操作。由于HTTP实际上是访问远程服务器的Web服务,因此它控制文件呈现给您的方式。对于许多Web服务器,您可以打开文件夹内容的“索引”。这会导致文件夹中的条目以HTML格式显示在浏览器中。为了遍历目录结构,您需要解析此HTML以查找路径信息。
如果您使用的是FTP,则可以将ftp://...
网址从5.0版开始传递给opendir()
。请注意,您的服务器管理员可以关闭此功能。如果您无法直接使用此功能,请参阅PHP的FTP functions手册(包括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);
?>
答案 1 :(得分:5)
对于公众可见的文件/文件夹,您可以使用此脚本执行此操作:没有FTP,没有任何困难。它将返回远程文件夹中的所有文件名。如果你可以用浏览器打开文件夹,你也可以用php阅读它。快乐的leeching;)
更新:这只列出了公众可用的文件,肯定不是仅对某个用户可见的文件!
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>';
}
答案 2 :(得分:3)
假设您在服务器上为php加载了ftp扩展,那么您应该可以使用phps ftp functions