Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 897
Tim Avatar asked Feb 17 '26 09:02

Tim


1 Answers

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());

like image 50
profitphp Avatar answered Feb 19 '26 00:02

profitphp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!