How to list files in a directory?

Asked

Viewed 717 times

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();
}

However when executing right after the first file appears a '.. ' and as my idea is to make it read and give include in each file (because they are classes) these '.. ' give error.

Someone knows how to tidy up or knows another method for my goal?

  • Augusto, avoid using snippet for nonexecutable code like html + javascript and/or css are..

  • But when pasting the problem code, only the first line enters...

3 answers

2

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

foreach ($dir as $fileInfo) {
   if(!$fileInfo->isDir()){
      $ext = strtolower( $fileInfo->getExtension() );
      if( in_array( $ext, $types ) ) echo $fileInfo->getFilename();
   }
}
  • n gave crt, he n displays nd ...

  • @Augustofurlan and if you change the if by if (!$fileinfo->isDot())

  • not right ...

2

You can use the method isDot() to check if 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’re giving include in various files, perhaps what you are looking for is the RecursiveDirectoryIterator.

With it you will be able to enter the subdirectories and give the include files (of course, checking before 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 they are classes, maybe you want to filter by PHP extension. For this you can combine with more others Iterators, like the 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 make a autoloader, instead try to use the Composer ;)

0


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

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

  • Augusto, again saying, avoid using snippet if the code is not executable, such as html with css and some javascript combined.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.