Help save directory and file in multidimensional array

0

I'm doing a code that allows me to list directories and files in a multidimensional array. The idea is to use an opendir function and with it return an array with two dimensions: $array [$is_dir] [$is_file] . I have a preliminary code:

    <body>        
        <?php 
        //función para obtener el nombre de las carpetas y los archivos en array multidimensional
        function dirToArray($dir) { 
            
            //creo un array
            $listDir = array();
            
            //abro los directorios contenidos en $dir
            if($handler = opendir($dir)) { 
                
                //leo todos los elementos contenidos 
                while (false !== ($file = readdir($handler))) { 
                    
                    //verifico que hayan elementos
                    if ($file != "." && $file != "..") {
                        
                        /*si los elementos son archivos, guardo los elementos 
                        en algún indice (dimensión) del array*/
                        if(is_file($dir."/".$file)) { 
                            $listDir[] = $file;
                            
                        /*si los elementos son directorios, guardo los elementos 
                        en otro índice o dimensión, repitiendo hasta que hayan elementos*/
                        }elseif(is_dir($dir."/".$file)){ 
                            $listDir[$file] = dirToArray ($dir."/".$file); 
                        } 
                    } 
                }
                closedir($handler); 
            } 
            return $listDir; 
        } 
       
        ?>
    </body> 

but when I try it, I get this error:

  

Notice: Undefined variable: listDir in C: ...

    
asked by virtual8870 08.11.2016 в 20:40
source

1 answer

0

This is the correct way to iterate over the directory:

while (false !== ($entrada = readdir($gestor))) {
    echo "$entrada\n";
}

link

    
answered by 08.11.2016 в 21:58