PHP:DirectoryIterator - 使用http地址,而不是绝对路径?

发布于 2024-10-06 18:09:40 字数 730 浏览 0 评论 0原文

首先,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2024-10-13 18:09:40

在回显 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());

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文