PHP: DirectoryIterator - using http adrress, not absolute path?
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?
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());
精彩评论