Folder navigation in PHP

4

I have some folders in my project and I need to go through all of them by saving the name of the mother folder, the daughter, and their files ...

The parent folder is "Manual", inside it I have other folders "Register" and "Reports", in these sub-folders I have some images (screen print), so I need to return an array with the data:

array[0][0] = "Manual";
array[0][1] = "Cadastro";
array[0][2] = "Imagem001.png";

array[1][0] = "Manual";
array[1][1] = "Cadastro";
array[1][2] = "Imagem002.png";

array[2][0] = "Manual";
array[2][1] = "Relatorio";
array[2][2] = "Imagem001.png";

array[3][0] = "Manual";
array[3][1] = "Relatorio";
array[3][2] = "Imagem001.png";

Does anyone know how to do this? I have tried using php dir but I could not get the parent folder ...

    
asked by anonymous 31.08.2015 / 19:14

2 answers

3
<?php
    function ListFolder($path){
        $dir_handle = opendir($path) or die("Erro");
        $dirname = end(explode("/", $path));

        echo ("<li> $dirname");
        echo "<ul>";
        while (false !== ($file = readdir($dir_handle))) {
            if($file!="." && $file!=".."){
                if (is_dir($path."/".$file)){
                    ListFolder($path."/".$file);
                }
                else{
                    echo "<li>$file</li>";
                }
            }
        }
        echo "</ul>";
        echo "</li>";

        closedir($dir_handle);
    }
?>

ListFolder('Manual');
    
31.08.2015 / 19:34
1

You can also use this method:

function gerenciarArquivos($path)
    {
        $files = glob($path."*.{jpg,png,gif}", GLOB_BRACE);
        $listFiles = array();

        if (count($files)) {
            $listFiles[] = "<ul>";
            foreach ($files as $file) {
              $listFiles[] = "<li>{$file}</li>";
            }
            $listFiles[] = "</ul>";
        }

        return implode("\n", $listFiles);
    }
echo gerenciarArquivos('/folder/');
    
31.08.2015 / 19:54