Folder browsing in PHP

Asked

Viewed 1,581 times

4

I have a few folders in my project and I need to go through them all keeping the name of the mother, daughter, and her files folder...

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’ve tried using php dir but I couldn’t get the parent folder...

  • Tried using a foreach() to access objects?

  • 2

    @Andrébaill The foreach I know how to use hehehe... the problem is accessing each windows directory.

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

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/');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.