How to list files in a directory?

1

I use the following code:

$path = 'pasta_desejada';
$dir = new DirectoryIterator($path);
foreach ($dir as $fileInfo) {
    $ext = strtolower( $fileInfo->getExtension() );
    if( in_array( $ext, $types ) ) echo $fileInfo->getFilename();
}

But when I run the file right after the first file it appears a '..' and as my idea is to make it read and give include to each file (because they are classes) those '..' give error.

Does anyone know how to fix or know another method for my goal?

    
asked by anonymous 25.08.2016 / 20:25

3 answers

0

I solved the autoload problem, I will answer my own question, because if anyone else needs to use it ...

function autoLoad() {
    blockAutoLoad('class/'); // invoca metodo de autoload
}


function blockAutoLoad($path) {
    include_once ($path . 'sysConnect.php'); // inclui a connect pois todas extend dela
    $dir = new DirectoryIterator($path); // acessa dir
    foreach ($dir as $fileInfo) { // pega file info
    //echo $fileInfo->getFilename() . "<br />";
    $arrayFileName = explode('.', $fileInfo->getFilename()); // tira os .
    foreach ($arrayFileName as $sl) { // foreach
            if($sl =='' || $sl == 'php' || $sl == ' ' || $sl == 'teste' || $sl == 'DS_Store') { // limpa string
                //echo $sl . '<br />';
            } else {
                //echo $sl . '<br />'; // exibe nome de arquivos
                include_once ($path . $sl . '.php'); // inclui classe
            }
        }
    }
}

I hope I can help someone ... XD

    
26.08.2016 / 01:56
2

Make sure $fileInfo is a file or directory, two methods can do this depending on the case isDir () and or isFile () .

foreach ($dir as $fileInfo) {
   if(!$fileInfo->isDir()){
      $ext = strtolower( $fileInfo->getExtension() );
      if( in_array( $ext, $types ) ) echo $fileInfo->getFilename();
   }
}
    
25.08.2016 / 20:37
2

You can use the isDot() method to verify that the item is a browser between directories.

<?php

$path = 'pasta_desejada';
$dir = new DirectoryIterator($path);

foreach ($dir as $item) {
    if (! $item->isDot()) {
        // Faz algo
    }
}

But since you are giving include to multiple files, perhaps what you are looking for is RecursiveDirectoryIterator .

With it you will be able to enter the subdirectories and give include in the files (of course, checking beforehand if they are files):

<?php

$path = 'pasta_desejada';
$dir = new RecursiveDirectoryIterator ($path);
$iterator = new RecursiveIteratorIterator($dir);

foreach ($iterator as $item) {
    // Verifica se é um arquivo
    if ($item->isFile()) {
        // Faz algo
    }
}

Since these are classes, you might want to filter by the PHP extension . For this you can combine with other Iterators , such as RegexIterator

<?php

$path = 'pasta_desejada';
$dir = new RecursiveDirectoryIterator ($path);
$iterator = new RecursiveIteratorIterator($dir);

$filterIterator= new RegexIterator(
    $iterator , 
    '/^.+\.php$/i', 
    RecursiveRegexIterator::GET_MATCH
);

foreach ($iterator as $item) {
    // Verifica se é um arquivo
    if ($item->isFile()) {
        // Faz algo
    }
}

There are several ways to do this. Just please do not try to reinvent the wheel. If you are trying to do an autoloader , instead try Composer ;)

    
26.08.2016 / 02:50