Ver Mensaje Individual
  #3 (permalink)  
Antiguo 24/12/2010, 10:37
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
Respuesta: [APORTE] Crear árbol de directorios

Inventando con el código, esto es lo que he podido hacer adicional. En este código le estoy indicando que lea solamente los archivos pdf, ustedes lo pueden colocar a la manera que quieran
index.php o index.html
Código HTML:
Ver original
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
  2. <frameset cols="20%,80%">
  3.   <frame src="directories.php" />
  4.   <frame src="blank.php" name="content" />
  5. </frameset>
  6. </html>

directories.php
Código PHP:
Ver original
  1. <?php require_once 'class.php'; ?>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <script type="text/javascript" src="script.js"></script>
  6. <link type="text/css" rel="stylesheet" href="style.css" />
  7. </head>
  8. <body>
  9.     <?php
  10.         $dirStructure = new DirStructure(array('pdf'));
  11.  
  12.         /**
  13.          * The next line you can specified the directory that you want to check,
  14.          * if not specified it will check the current directory
  15.          */
  16.         echo $dirStructure->getFile($dirStructure->setFile($dirStructure->path('docs')));
  17.     ?>
  18. <div id="tree_notification">Cargando...</div>
  19. </body>
  20. </html>

class.php
Código PHP:
Ver original
  1. <?php
  2. class DirStructure{
  3.     /**
  4.      * Avoid using .. /, so you can not go back
  5.      * Default false
  6.      */
  7.     private $_safeMode = false;
  8.  
  9.     /**
  10.      * Hide all directories that do not have any files,
  11.      * even if there are other directories that are empty
  12.      * Default true
  13.      */
  14.     private $_hideEmptyDir = true;
  15.  
  16.     /**
  17.      * Show only this extentions. Format: mime type
  18.      * application/pdf              PDF
  19.      * application/zip              DOCX|XLSX|PPTX|...
  20.      * application/octet-stream         ZIP
  21.      * application/x-rar            RAR
  22.      * text/x-php               PHP
  23.      * text/html                    HTML/HTM
  24.      * application/x-empty          (empty files)
  25.      *
  26.      * Example of how it will be set:
  27.      * $_fInfoExt = array('application/pdf', 'application/zip', 'text/x-php');
  28.      *
  29.      * Leave blank if you want to display all
  30.      * $_fInfoExt = array();
  31.      *
  32.      * @param array $array
  33.      */
  34.     private $_realExt = array(
  35.                             'pdf' => 'application/pdf',
  36.                             'docx' => 'application/zip',
  37.                             'xlsx' => 'application/zip',
  38.                             'pptx' => 'application/zip',
  39.                             'zip' => 'application/octet-stream',
  40.                             'rar' => 'application/x-rar',
  41.                             'php' => 'text/x-php',
  42.                             'html' => 'text/html',
  43.                             'empty files' => 'application/x-empty',
  44.                         );
  45.     private $_fInfoExt = array();
  46.  
  47.     public function __construct(array $array = array()){
  48.         foreach($array as $value){
  49.             $this->_fInfoExt[] = array_key_exists($value, $this->_realExt)
  50.                 ? $this->_realExt[$value]
  51.                 : $value;
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * Set path for current directory so this scripts works if include in another php file
  57.      *
  58.      * @param string $dir
  59.      * @return string
  60.      */
  61.     public function path($dir){
  62.         if($this->_safeMode){ $dir = str_replace('../', '', $dir); }
  63.         return realpath(__DIR__ . DIRECTORY_SEPARATOR . $dir);
  64.     }
  65.  
  66.     /**
  67.      * Check for real mime extention
  68.      *
  69.      * @param string $fileUrl Set file to check
  70.      * @return string
  71.      */
  72.     private function _whatMimeExt($fileUrl = null){
  73.         if(empty($fileUrl)){ return; }
  74.  
  75.         $fInfo = finfo_open(FILEINFO_MIME_TYPE);
  76.         $mimeExtention = finfo_file($fInfo, $fileUrl);
  77.         finfo_close($fInfo);
  78.  
  79.         return $mimeExtention;
  80.     }
  81.  
  82.     /**
  83.      * Check if is real extention
  84.      *
  85.      * @param string $fileUrl Set file to check
  86.      * @param array $ext Extention(s) to check. Format: Mime type
  87.      * @return bool
  88.      */
  89.     private function _realExtFile($fileUrl, array $ext = array()){
  90.         if(empty($ext)){ return false; }
  91.  
  92.         foreach($ext as $e){
  93.             if(is_file($fileUrl) && $this->_whatMimeExt($fileUrl) != $e){ return true; }
  94.         }
  95.  
  96.         return false;
  97.     }
  98.  
  99.     /**
  100.      * Get the size of directory. If $_fInfoExt is specified, only get size of directory that has what was indicated.
  101.      *
  102.      * @param string $dir Directory name
  103.      * @return integer
  104.      */
  105.     private function _dirSize($dir){
  106.         $size = 0;
  107.  
  108.         foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $file){
  109.             if($this->_realExtFile(realPath($file->getPathname()), $this->_fInfoExt)){ continue; }
  110.             $size+=$file->getSize();
  111.         }
  112.         return $size;
  113.     }
  114.  
  115.     /**
  116.      * Set array with the structure of directories and files
  117.      *
  118.      * @param string $root
  119.      * @param int $n
  120.      * @return array
  121.      */
  122.     public function setFile($root, $n = 0){
  123.         $onlyDirs = array();
  124.         $onlyFiles = array();
  125.         $n++;
  126.  
  127.         foreach(glob($root . DIRECTORY_SEPARATOR . '*') as $file){
  128.             /**
  129.              * Check if dir is empty, do not display and if is
  130.              */
  131.             if($this->_hideEmptyDir && is_dir($file) && $this->_dirSize($file) == 0){ continue; }
  132.  
  133.             /**
  134.              * Set info of file
  135.              */
  136.             $f = pathInfo($file);
  137.  
  138.             if(is_dir($file)){
  139.                 /**
  140.                  * Check if dir name is only number(s), append a space
  141.                  * to the end of the name to mantain the key when using array_merge.
  142.                  */
  143.                 $key = is_numeric($f['basename'])
  144.                     ? $f['basename'] . ' '
  145.                     : $f['basename'];
  146.                 $onlyDirs[$key] = array(
  147.                                     'real_path' => realpath($f['dirname'] . DIRECTORY_SEPARATOR . $f['basename']),
  148.                                     'n_array' => $n,
  149.                                     'files_in_dir' => $this->setFile($file, $n)
  150.                                 );
  151.             }
  152.             else{
  153.                 $onlyFiles[$f['basename']] = array(
  154.                                                 'real_path' => realpath($f['dirname'] . DIRECTORY_SEPARATOR . $f['basename']),
  155.                                                 'http_path' => '.' . str_replace(array('\\', '//'), '/', str_replace(getcwd(), '', $f['dirname'])) . '/' . $f['basename'],
  156.                                                 'n_array' => $n,
  157.                                                 'pathInfo' => pathInfo($file),
  158.                                                 'timeModified' => date('m-d-Y H:i:s', filemtime($file))
  159.                                             );
  160.             }
  161.         }
  162.         return array_merge($onlyDirs, $onlyFiles);
  163.     }
  164.  
  165.     /**
  166.      * Organize array given of setFile()
  167.      *
  168.      * @param array $files
  169.      * @param string $class Show or hide directory info, values: show|hide Default: show (for firsts directories)
  170.      * @return string
  171.      */
  172.     public function getFile($files, $class='show'){
  173.         $customFile = '';
  174.  
  175.         foreach($files as $key => $file){
  176.             if($this->_realExtFile($file['real_path'], $this->_fInfoExt)){ continue; }
  177.  
  178.             if(is_array($file) && $key != 'pathInfo'){
  179.                 if($key != 'files_in_dir'){
  180.                     /**
  181.                      * Check if is directory or file and if is a file you can add more mime extentions
  182.                      * for more info about mime extention check the method setFInfoExt()
  183.                      */
  184.                     switch($this->_whatMimeExt($file['real_path'])){
  185.                         case 'directory':
  186.                             $img = '<img src="images/dir.png" width="20" height="20" alt="' . trim($key) . '" title="' . trim($key) . '" />';
  187.                             break;
  188.                         case 'application/pdf':
  189.                             $img = '<img src="images/pdf.png" width="20" height="20" alt="' . trim($key) . '" title="' . trim($key) . '" />';
  190.                             break;
  191.                         default:
  192.                             $img = '<img src="images/file.png" width="14" height="20" alt="' . trim($key) . '" title="' . trim($key) . '" />';
  193.                     }
  194.  
  195.                     $tab = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $file['n_array']);
  196.                     $link = (empty($file['http_path'])
  197.                         ? '<a href="javascript: void(0);" onclick="hideUnhideMenu(this)" >' . $img . trim($key) . '</a>'
  198.                         : '<a href="' . $file['http_path'] . '" target="content" onclick="'
  199.                             . 'hideUnhideExtras(\'tree_notification\');'
  200.                             . ' setTimeout(\'hideUnhideExtras(\\\'tree_notification\\\')\', 1500);'
  201.                             . '">' . $img . ' ' . trim($key) . '</a>'
  202.                     );
  203.                     $customFile .= $tab . ' ' . $link . '<br />';
  204.                 }
  205.                 if(!empty($file['files_in_dir'])){
  206.                     $customFile .=  $this->getFile($file['files_in_dir'], 'hide');
  207.                 }
  208.             }
  209.         }
  210.  
  211.         return '<div class="tree_' . $class . '">' . $customFile . '</div>';
  212.     }
  213. }

style.css
Código CSS:
Ver original
  1. * { padding: 0; margin: 0; }
  2. html, body{ width: 100%; height: 100%; }
  3. body {
  4.     background: #C3C3C3;
  5.     font-family: Arial, Helvetica, sans-serif;
  6.     font-size: 13px;
  7.     color: #666666;
  8. }
  9.  
  10. a{
  11.     color: #369;
  12.     text-decoration: none;
  13.     font-weight: bold;
  14. }
  15. a:hover{ color: #900; }
  16. img{ border: none; }
  17.  
  18. /*tree*/
  19. .tree_hide{ display: none; }
  20. .tree_show{ display: block; }
  21. #tree_notification{
  22.     background-color: #6886ac;
  23.     padding: 4px 5px 5px 5px;
  24.     position: absolute;
  25.     top: 0;
  26.     right: 0;
  27.     color: #fff;
  28.     font-size: 14px;
  29.     display: none;
  30. }

script.js
Código Javascript:
Ver original
  1. function hideUnhideMenu(obj){
  2.     obj = obj.nextSibling;
  3.     while(obj.nodeName != 'DIV'){
  4.         obj = obj.nextSibling
  5.     }
  6.     var thisElement = obj.style;
  7.     if(thisElement.display == 'block'){ thisElement.display = 'none'; }
  8.     else{ thisElement.display = 'block'; }
  9. }
  10.  
  11. function hideUnhideExtras(id){
  12.     var thisElement = document.getElementById(id).style;
  13.     if(thisElement.display == 'block'){ thisElement.display = 'none'; }
  14.     else{ thisElement.display = 'block'; }
  15. }

blank.php o blank.html es un archivo vacio, para evitar errores en los frames. Crean un directorio llamado images y colocan las siguientes imagenes

dir.png
pdf.png
file.png

Edito: Necesitan tener activa la extensión php_fileinfo

Nota:
12-24-2010 - se añadió la variable $safe_mode, sugerida por miktrv.
12-28-2010 - se cambió el nombre de la variable $safe_mode a $_safeMode y se creó la variable $_hideEmptyDir. Se modificó el código para que en el construct se indicara las extensiones a mostrar
__________________
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; 28/12/2010 a las 06:26