Julez
Julez

Reputation: 1369

Recursive directory iterator get filepath of file with subfolders

i have a function that recursively iterates over a directory tree. This works fine, i get a array returned with the directorys and files.

                $dir = 'files';
                function dirToArray($dir) { 

                   $result = array(); 

                   $cdir = scandir($dir); 
                   foreach ($cdir as $key => $value) {

                      if (!in_array($value,array(".",".."))) { 
                         if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) { 
                            $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value); 
                         } 
                         else { 
                            $result[] = $value;
                         } 
                      } 
                   } 

                   return $result; 
                }

i then iterate recursivly over the returnted array to display i nice menu structure to the user, wich also works fine.

                function printAll($arr) {

                    if(!is_array($arr)) {
                        echo '<li><div class="sub_menu_path"></div><a href="'.$arr.'" class="file"><i class="fa fa-fw fa-file-text-o"></i>'.$arr.'</a></li>';
                    }
                    else {

                        foreach($arr as $k => $v) {

                            if(is_array($v)) {  
                                echo '<li><div class="sub_menu_path"></div><a href="'.$k.'" class="multi_menu folder"><i class="fa fa-fw fa-folder-o"></i>'.$k.'</a>';                          

                                echo '<ul class="sub_menu">';                               
                                    printAll($v);                               
                                echo '</ul>';

                                echo '</li>';
                            }
                            else {
                                echo '<li><div class="sub_menu_path"></div><a href="'.$v.'" class="file"><i class="fa fa-fw fa-file-text-o"></i>'.$v.'</a></li>';                                   
                            }
                        }
                    }
                }               

                printall(dirToArray($dir));

result:

enter image description here

but what i want is when i display the file to the user wrapt in a html <a href="$value">$value</a> how to get the complete path including subfolders? for example: files/Products/Cress/Ramnunculus/Introduction/file.txt i only get file.txt so when i click in file.txt it says file not found ofcourse.

EDIT (Solution):

as suggested in the answer, i added a extra parameter. i changed the variable $v to $k because it gives a array to string conversion.

            function printAll($arr, $path = '') {

                    if(!is_array($arr)) {
                        echo '<li><div class="sub_menu_path"></div><a href="'.$arr.'" class="file"><i class="fa fa-fw fa-file-text-o"></i>'.$arr.'</a></li>';
                    }
                    else {

                        foreach($arr as $k => $v) {

                            $file_path = $path . ($path == '' ? '' : '/') . $k;

                            if(is_array($v)) {  
                                echo '<li><div class="sub_menu_path"></div><a href="" class="multi_menu folder"><i class="fa fa-fw fa-folder-o"></i>'.$k.'</a>';

                                echo '<ul class="sub_menu">';                               
                                    printAll($v, $file_path);                               
                                echo '</ul>';

                                echo '</li>';
                            }
                            else {
                                echo '<li><div class="sub_menu_path"></div><a href="'.$file_path.'" class="file"><i class="fa fa-fw fa-file-text-o"></i>'.$v.'</a></li>';                                   
                            }
                        }
                    }
                }               

                printall(dirToArray($dir));

Upvotes: 0

Views: 378

Answers (1)

Siffer
Siffer

Reputation: 353

Include optional parameter $path = ''

    function printAll($arr, $path = '') {

        if(!is_array($arr)) {
            $file_path = $path . ($path == '' ? '' : '/') . $arr;
            echo '<li><div class="sub_menu_path"></div><a href="'.$file_path.'" class="file"><i class="fa fa-fw fa-file-text-o"></i>'.$arr.'</a></li>';
        }
        else {

            foreach($arr as $k => $v) {
                if(is_array($v)) {
                    $file_path = $path . ($path == '' ? '' : '/') . $k;

                    echo '<li><div class="sub_menu_path"></div><a href="'.$file_path.'" class="multi_menu folder"><i class="fa fa-fw fa-folder-o"></i>'.$k.'</a>';

                    echo '<ul class="sub_menu">';
                    printAll($v, $file_path);
                    echo '</ul>';

                    echo '</li>';
                }
                else {
                    $file_path = $path . ($path == '' ? '' : '/') . $v;
                    echo '<li><div class="sub_menu_path"></div><a href="'.$file_path.'" class="file"><i class="fa fa-fw fa-file-text-o"></i>'.$v.'</a></li>';
                }
            }
        }
    }

Upvotes: 2

Related Questions