Guys,
Goodnight.
I'm going to show you how to list files in a directory in PHP using 2 functions and then, natively. I will create a class with static methods so I don't need to instantiate the object.
<?php
class clsArquivo
{
public static function pegaExtensao( $nomeArquivo )
{
$posicao = strrpos( $nomeArquivo, "." );
$extensao = strtolower( substr( $nomeArquivo, $posicao + 1 ) );
return $extensao;
}
public static function listarArquivos( $strDiretorio, $vetExtensoes = null, $bolRecursivo = false, $bolOrdenacaoDecrescente = false )
{
if ( is_dir( $strDiretorio ) )
{
$vetor = array();
$strDiretorio .= ( substr( $strDiretorio, -1 ) != "/" ) ? "/" : null;
$d = dir( $strDiretorio );
while ( false !== ( $arquivo = $d->read() ) )
{
if ( is_dir( $strDiretorio . $arquivo ) )
{
if ( $bolRecursivo && $arquivo != "." && $arquivo != ".." )
{
$arquivo = $strDiretorio . $arquivo . "/";
$vetorResultante = self::listarArquivos( $arquivo, $vetExtensoes, true );
if ( is_array( $vetorResultante ) && count( $vetorResultante ) > 0 )
{
$vetor = array_merge( $vetor, $vetorResultante );
}
}
}
else
{
if ( is_array( $vetExtensoes ) )
{
$ext = self::pegaExtensao( $arquivo );
if ( in_array( $ext, $vetExtensoes ) )
{
$strArquivo = $strDiretorio . $arquivo;
if ( substr( $strArquivo, 0, 2 ) == "./" )
{
$strArquivo = substr( $strArquivo, 2 );
}
if ( is_file( $strArquivo ) )
{
$vetor[] = $strArquivo;
}
}
}
else
{
$strArquivo = $strDiretorio . $arquivo;
if ( substr( $strArquivo, 0, 2 ) == "./" )
{
$strArquivo = substr( $strArquivo, 2 );
}
if ( is_file( $strArquivo ) )
{
$vetor[] = $strArquivo;
}
}
}
}
$d->close();
if ( count( $vetor ) > 0 )
{
if ( $bolOrdenacaoDecrescente )
{
rsort( $vetor );
}
else
{
usort( $vetor, 'strnatcasecmp' );
}
}
return $vetor;
}
}
}
How to use:
//Carregando a nossa classe
require("classes/clsArquivo.php");
//Definindo as extensões de arquivos que estamos procurando
$extensoes = array("txt", "log", "php");
/* Executa a função e retorna um vetor com o caminho dos arquivos encontrados nas pastas
e subpastas do diretório "C:\Diretorio\", que será associado à variável $arrArquivos */
$arrArquivos = clsArquivo::listarArquivos("C:\Diretorio\", $extensoes, true);
//Exibindo os valores do vetor na tela
var_dump($arrArquivos);
The example above will print something similar to:
array
0 => string 'C:\Diretorio\index.php' (length=70)
1 => string 'C:\Diretorio\wp-activate.php' (length=76)
2 => string 'C:\Diretorio\wp-blog-header.php' (length=79)
3 => string 'C:\Diretorio\wp-comments-post.php' (length=81)
4 => string 'C:\Diretorio\wp-config-sample.php' (length=81)
5 => string 'C:\Diretorio\wp-cron.php' (length=72)
6 => string 'C:\Diretorio\wp-links-opml.php' (length=78)
7 => string 'C:\Diretorio\wp-load.php' (length=72)
8 => string 'C:\Diretorio\wp-login.php' (length=73)
9 => string 'C:\Diretorio\wp-mail.php' (length=72)
10 => string 'C:\Diretorio\wp-settings.php' (length=76)
11 => string 'C:\Diretorio\wp-signup.php' (length=74)
12 => string 'C:\Diretorio\wp-trackback.php' (length=77)
13 => string 'C:\Diretorio\xmlrpc.php' (length=71)
Now let's see using the native function glob from PHP:
<?php
foreach (glob("*.txt") as $arquivo) {
echo "tamanho de $arquivo " . filesize($arquivo) . "\n";
}
?>
The example above will print something similar to:
size of funclist.txt 44686
size of funcsummary.txt 267625
size of quickref.txt 137820
For more information about the glob function, visit http://www.php.net/manual/pt_BR/function.glob.php
That's it!!
Comentários (0)
Carregando comentários…