Ver Mensaje Individual
  #2 (permalink)  
Antiguo 17/03/2011, 13:00
Avatar de masterpuppet
masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: listar archivos de una carpeta empezados por un nombre

Que tal Masterphp,

Si dispones de php 5.1+ podrias utilizar FilterIterator, te dejo un ejemplo de una clase básica.
Código PHP:
Ver original
  1. class CustomFileIterator extends FilterIterator
  2. {
  3.  
  4.     /**
  5.      * @var string
  6.      */
  7.     protected $_pattern;
  8.  
  9.     /**
  10.      * CustomFileIterator constructor
  11.      *
  12.      * @param  string|DirectoryIterator $dirOrIterator
  13.      * @return void
  14.      * @throws InvalidArgumentException
  15.      */
  16.     public function __construct($pattern, $dirOrIterator = '.')
  17.     {
  18.         if (is_string($dirOrIterator)) {
  19.             if (!is_dir($dirOrIterator)) {
  20.                 throw new InvalidArgumentException('Expected a valid directory name');
  21.             }
  22.  
  23.             $dirOrIterator = new RecursiveDirectoryIterator($dirOrIterator);
  24.         }
  25.         if (!$dirOrIterator instanceof DirectoryIterator) {
  26.             throw new InvalidArgumentException('Expected a DirectoryIterator');
  27.         }
  28.  
  29.         if ($dirOrIterator instanceof RecursiveIterator) {
  30.             $iterator = new RecursiveIteratorIterator($dirOrIterator);
  31.         } else {
  32.             $iterator = $dirOrIterator;
  33.         }
  34.         parent::__construct($iterator);
  35.         $this->_pattern = $pattern;
  36.         $this->rewind();
  37.     }
  38.  
  39.     /**
  40.      * @return boolean
  41.      */
  42.     public function accept()
  43.     {
  44.         $file = $this->getInnerIterator()->current();
  45.  
  46.         if (!$file instanceof \SplFileInfo
  47.             || !$file->isFile()
  48.             ) {
  49.             return false;
  50.         }
  51.  
  52.         if ($file->getBasename('.php') == $file->getBasename()) {
  53.             return false;
  54.         }
  55.  
  56.         return preg_match($this->_pattern, $file->getFilename());
  57.     }
  58. }

y para utilizarla:

Código PHP:
Ver original
  1. try {
  2.     $it = new CustomFileIterator('/^block_/', '/path/to/directory');
  3.     foreach($it as $file){
  4.         echo $file->getFilename() . PHP_EOL;
  5.     }
  6. } catch(InvalidArgumentException $e) {
  7.     echo $e->getMessage() . PHP_EOL;
  8. }

Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)