PHP:DirectoryIterator - 使用http地址,而不是绝对路径?
首先,我使用 Server2Go 创建一个基于 CD-ROM 的站点。
我正在尝试使用 DirectoryIterator 创建导航栏,直接取自 .php 文件的文件夹/文件结构。这是我的代码:
<?php
$root = $_ENV["S2G_SERVER_DOCROOT"]."/content/";
$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($root));
foreach($files as $file){
echo '<li><a href=' . $file->getPathname() . '>' . $file->getPathname() . PHP_EOL . '</a></li>';
}
?>
这样做的问题是它输出每个文件夹/文件的完整绝对路径(即c:/等),这会导致.php文件无法打开的问题,因为它们只能打开来自基于 http 的 URL。我需要它做的是将路径输出为 http:// 路径或相对于 Web 根目录的路径。还有另一个名为 S2G_BASE_URL 的 Server2Go ENV 变量,在本例中它为您提供了 webroot hHttp://127.0.0.1:80 ),但我不能将其与 DirectortIterator 一起使用,因为它不适用于 http 地址,它需要文档路径。
有人对我如何做到这一点有任何想法吗?
First of all, I'm creating a CD-ROM based site, using Server2Go.
I'm trying to use DirectoryIterator
to create a navigation bar, taken straight from my folder/file structure of .php files. Here's my code:
<?php
$root = $_ENV["S2G_SERVER_DOCROOT"]."/content/";
$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($root));
foreach($files as $file){
echo '<li><a href=' . $file->getPathname() . '>' . $file->getPathname() . PHP_EOL . '</a></li>';
}
?>
The problem with this is that it outputs the full absolute path for each folder / file (i.e. c:/ etc. etc.), which causes the problem that the .php files don't open as they can only open from a http based URL. What I need it to do is output the paths as either http:// paths, or relative to the web root. There is another Server2Go ENV varialbe called S2G_BASE_URL which gives you your webroot hHttp://127.0.0.1:80 in this case), but I can't use that with DirectortIterator as it doesn't work with http addresses, it needs document paths.
Does anyone have any thoughts on how I could do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在回显 getPathname 方法的输出之前,您必须替换字符串中的 $_SERVER["DOCUMENT_ROOT"] 。
<代码>
$path=str_replace($_SERVER["DOCUMENT_ROOT"],'',$file->getPathname());
Before you echo the output from the getPathname method you'll have to replace $_SERVER["DOCUMENT_ROOT"] in the string.
$path=str_replace($_SERVER["DOCUMENT_ROOT"],'',$file->getPathname());