Programación

PHP

Listado de archivos por carpeta

Última actualización: 29-03-2019 19:25

Clase PHP cl_fileListGenerator

También tiene este par de propiedades: La función getFileListFromFolder que genera la lista HTML de una carpeta recibe este parámetro:
<?php
    class cl_fileListGenerator
    {
        private $pathToStart = '';
        var $pfn = '';
        var $path = '';
        var $fileName= '';
        var $nFiles = 0;
        var $maxNFiles = 0;
 
        var $result = '';
        var $tab = '';
        var $bytes = 0;
 
        function __construct() { $this->tab = str_repeat('&nbsp;', 5); }
 
        function addToFileList($path, $level = 0)
        {
            if ($this->nFiles >= $this->maxNFiles && $this->maxNFiles != 0) return;
 
            if (is_dir($path))
            {
                if ($xDir = opendir($path))
                {
                    while (($item = readdir($xDir)) !== false)
                    {
 
                        if ($item != "." && $item != "..")
                        {
                            if (is_dir($path . $item)) $this->addToFileList($path . $item . "/", $level + 1);
                            elseif (is_file($path . $item))
                            {
 
                                $this->nFiles++;
                                if ($this->nFiles > $this->maxNFiles && $this->maxNFiles != 0) break;
                                $fileSize = filesize($path . $item);
                                $this->bytes += $fileSize;
                                $this->result .= '<tr'
                                                        . ($this->nFiles % 2 == 0 ? ' style="background-color:#EEE;"' : '')
                                                    . '>'
                                        . '<td>' . $level . '</td>'
                                        . '<td>'
                                                . ($level == 0 ? '' : str_repeat($this->tab, $level))
                                                . $path
                                        . '</td>'
                                        . '<td>' . $item . ' </td>'
                                        . '<td>' . $fileSize . '</td>'
                                        . '<td>' . round($fileSize / 1024, 2) . '</td>'
                                        . '<td>' . round($fileSize / 1024 / 1024, 2) . '</td>'
                                    . '</tr>';
 
 
                            }
                        }
                    }
                    closedir($xDir);
                }
            }
 
            return $this->result;
        }
 
        function getFileListFromFolder($pathToStart)
        {
            $this->result = '<table style="width:100%;">'
                    . '<thead' . ' style="background-color:#000; color:#FFF;"' . '>'
                        . ' <tr>'
                            . '<th>Nivel</th>'
                            . '<th>Ruta</th>'
                            . '<th>Item</th>'
                            . '<th>Bytes</th>'
                            . '<th>KBytes</th>'
                            . '<th>MBytes</th>'
                        . '</tr>'
                    . '</thead>'
                . '<tbody>';
            $this->pfn = '';
            $this->pathToStart = $pathToStart;
            return $this->addToFileList($pathToStart, 0)
                    . '</tbody>'
                    . '<tfoot' . ' style="background-color:#000; color:#FFF;"' . '>'
                        . '<tr>'
                            . str_repeat('<td>&nbsp;</td>', 3)
                            . '<td>' . $this->bytes . '<br/>bytes' . '</td>'
                            . '<td>' . round($this->bytes / 1024, 2) . '<br/>KBytes' . '</td>'
                            . '<td>' . round($this->bytes / 1024 / 1024, 2) . '<br/>MBytes' . '</td>'
                        . '</tr>'
                    . '</tfoot>'
                . '</table>';
        }
    }
 
?>

 

Ejemplo de uso
<?php
    // Tiempo límite: 5 minutos:
    set_time_limit(5 * 60);
 
    // Para el ejemplo se utilizará la carpeta "Libros"
    $path = 'd:\\Escritorio\Stuff\PCID\Libros';
 
    // Instancia de clase cl_fileListGenerator y asignación de cantidad máxima
    // de archivos como ilimitada.
    $o = new cl_fileListGenerator();
    $o->maxNFiles = 0;
 
    echo $o->getFileListFromFolder( $path . '/');
    echo '<p>' . $o->nFiles . ' archivo' . ($o->nFiles == 1 ? '' : 's') . '.' . '</p>';
 
?>
Resultado