我有一个 url,http://www.mysite.com/images
和 images
目录允许目录列表。我如何使用 PHP 获取该目录中的文件?
最佳答案
如果您需要通过 HTTP 读取图像并且服务器是 Apache,这是一个示例:
<?php
$url = 'http://www.mysite.com/images';
$html = file_get_contents($url);
$count = preg_match_all('/<td><a href="([^"]+)">[^<]*<\/a><\/td>/i', $html, $files);
for ($i = 0; $i < $count; ++$i) {
echo "File: " . $files[1][$i] . "<br />\n";
}
?>
如果它是您运行 PHP 的同一台服务器,您可以使用 opendir() 和 readdir()。
关于php - 如何使用 PHP 读取远程服务器上的目录内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7437480/