0

Related to this post: "PHP list all files in directory" , specifically the code displayed by "Apr 19 '12 at 12:24 " Kiko"

I was wondering if anyone can suggest how to determine the folder path to the file itself: i.e.

$html .= "<li><span class='file'><a href='???' target='_blank'>". $eachDirectory ."</a></span></li>";

S I can wrap an HREF around it so it. The functions are recursive so I'm not sure at which stage I can determine the path of the file. Anyone have a way to do this? Thanks!

Community
  • 1
  • 1
mckere
  • 3
  • 2
  • You mean like the `$_SERVER['DOCUMENT_ROOT']`? – Sterling Archer Sep 25 '13 at 16:42
  • Could you give us a better example or elaborate more on exactly what you're trying to do? May be just me, but it doesn't seem very apparent. – Nick Rolando Sep 25 '13 at 16:46
  • Sorry for the confusion. The example provided by Kiko displayed the tree perfectly - I was looking for how to determine the path to only the "file" nodes so I could wrap them with an anchor tag, so people couple drill down to the files they wanted , then open it in a new window by clicking on a link. It wasn't apparent how to determine the path because of the recursion. See Brian Kinyua's post below - that's exactly what I was looking to do. – mckere Sep 25 '13 at 18:41

1 Answers1

1

I've looked at the referenced code and I have modified it to suite your needs. Replace ONLY the createTree function with this and try it

function createTree($directory,$parent = '')
{
    $html = "<ul>";

    foreach($directory as $keyDirectory => $eachDirectory)
    {
        if(is_array($eachDirectory))
        {
            $html .= "<li class='closed'><span class='folder'>" . $keyDirectory . "</span>";
            $html .= createTree($eachDirectory,$keyDirectory);
            $html .=  "</li>";
        }
        else
        {
            $origin = 'imgs';
            if(trim($parent) !== ''){
                $path = $origin . '/' . $parent . "/" .$eachDirectory;
            }else{
                $path = $origin . '/' . $eachDirectory;
            }
            $html .= "<li><span class='file'><a href='" . $path . "' target='_blank'>". $eachDirectory ."</a></span></li>";

        }
    }
    $html .= "</ul>";

    return $html;
}

You will notice that I've added a new argument item to the argument list of that function called $parent. This is used to carry the folder name of the parent folder of the item that will be linked. The folder name is then appended to the $origin variable (which is the name of the folder you traversed using the getFolderTree function , in my case it's 'imgs'.

There is an if-else statement that checks if the $parent is empty, it's pretty self explanatory from there. If you want clarification please ask.

NOTE 1: You need to replace the $origin value to the name of the folder at the root to get full paths

NOTE 2: You need to replace the forwardslash (/) to that of your system e.g. for *NIX it's a backslash so for PHP you need to use a double-backslash when working inside of strings but if you're on Windows my edit will work fine

Brian Kinyua
  • 4,456
  • 1
  • 18
  • 17
  • This is exactly what I was trying to do Brian Kinyua. It works perfectly. Thanks. I gave myself a headache last night trying to get this to work! – mckere Sep 25 '13 at 18:43
  • This works great for one level of depth (which at this point is all I have) i.e. root\Folder1\file1.txt which produces a url of root/Folder1/file1.txt but if you go down another level i.e. root\Folder1\subfolder\file2.txt the url generated for the link is: root/subfolder/file2.txt - it loses the Folder1 part. Have you got any idea how to have it work down n+ subdirectories? – mckere Sep 25 '13 at 19:18
  • Great. It worked for your current situation. Let me check the function to extend it for deeper levels. – Brian Kinyua Sep 26 '13 at 05:30