Ver Mensaje Individual
  #1 (permalink)  
Antiguo 29/11/2010, 07:17
Avatar de abimaelrc
abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
[APORTE] Crear árbol de directorios

Una forma sencilla de crear un árbol de directorios con PHP
Código PHP:
Ver original
  1. <?php
  2. if(!defined('PATH')){
  3.     define('PATH', __DIR__ . DIRECTORY_SEPARATOR);
  4. }
  5.  
  6. function getFile($root, $n = 0){
  7.     $onlyDirs = array();
  8.     $onlyFiles = array();
  9.     $n++;
  10.  
  11.     foreach(glob($root . '/*', GLOB_NOSORT) as $file){
  12.         $f = pathInfo($file);
  13.         if(is_dir($file)){
  14.             /**
  15.              * Verifica si el nombre del directorio es solo número, añade un espacio
  16.              * al final del nombre para mantener la llave cuando use array_merge
  17.              */
  18.             $key = is_numeric($f['basename']) ? $f['basename'] . ' ' : $f['basename'];
  19.             $onlyDirs[$key] = array(
  20.                                 'real_path' => realpath($f['dirname'] . DIRECTORY_SEPARATOR . $f['basename']),
  21.                                 'n_array' => $n,
  22.                                 'files_in_dir' => getFile($file, $n)
  23.                             );
  24.         }
  25.         else{
  26.             $onlyFiles[$f['basename']] = array(
  27.                                             'real_path' => realpath($f['dirname'] . DIRECTORY_SEPARATOR . $f['basename']),
  28.                                             'http_path' => str_replace(array('//', '\\'), '/', $f['dirname'] . '/') . $f['basename'],
  29.                                             'n_array' => $n,
  30.                                             'pathInfo' => pathInfo($file),
  31.                                             'timeModified' => date('m-d-Y H:i:s', filemtime($file))
  32.                                         );
  33.         }
  34.     }
  35.     return array_merge($onlyDirs, $onlyFiles);
  36. }
  37.  
  38. function setFile($files){
  39.     $customFile = '';
  40.     foreach($files as $key => $file){
  41.         if(is_array($file) && $key != 'pathInfo'){
  42.             if($key != 'files_in_dir'){
  43.                 $tab = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $file['n_array']);
  44.                 $type = empty($file['pathInfo']) ? '[' . trim($key) . ']' : trim($key);
  45.                 $customFile .= $tab . $type . '<br />';
  46.             }
  47.             $customFile .= setFile($file);
  48.         }
  49.     }
  50.     return $customFile;
  51. }
  52.  
  53. /**
  54.  * Puedes especificar un directorio o simplemente,
  55.  * lo dejas vacío para verificar desde donde está corriendo el código
  56.  */
  57. echo setFile(getFile(PATH . 'directorio'));

Nota:
2010-12-03: Corregir código
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos

Última edición por abimaelrc; 09/03/2011 a las 16:11 Razón: Correcciones