Guys,
Goodnight!
This time I come to comment on a problem that bothered me a little in PHP whenever I needed to delete a directory and it contained files.. As you know, the function rmdir does not delete directories if files exist. Well, let's work on a solution for that:
<?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;
}
}
public static function removerArquivosDoDiretorio( $strDiretorio, $vetExtensoes = null, $bolBuscaRecursiva = true )
{
$intSucessos = 0;
$vetArquivos = self::listarArquivos( $strDiretorio, $vetExtensoes, $bolBuscaRecursiva );
$numArquivos = count( $vetArquivos );
for ( $i = 0; $i < $numArquivos; $i++ )
{
$arquivo = $vetArquivos[$i];
if ( unlink( $arquivo ) )
{
$intSucessos++;
}
}
return ($intSucessos == $numArquivos);
}
public static function removerDiretorio( $strDiretorio, $bolBuscaRecursiva = true )
{
if ( is_dir( $strDiretorio ) )
{
self::removerArquivosDoDiretorio( $strDiretorio, null, $bolBuscaRecursiva );
$objects = scandir( $strDiretorio );
foreach ( $objects as $object )
{
if ( $object != "." && $object != ".." )
{
if ( filetype( $strDiretorio . "/" . $object ) == "dir" )
{
rmdir( $strDiretorio . "/" . $object );
}
}
}
reset( $objects );
return rmdir( $strDiretorio );
}
return false;
}
}
?>
Some functions of this class I already mentioned in the post Listing files with PHP, such as listFiles and catchExtension. Therefore, I will focus on new functions. Using the class is very simple for our problem:
//Carregar a nossa classe para a memória
require_once("classes/clsArquivo.php");
//Apagar todos os arquivos do diretório
clsArquivo::removerArquivosDoDiretorio("C:\Diretorio");
//Apagar todos os arquivos das extensões .txt e .log, incluindo subpastas
$extensoes = array("txt", "log");
clsArquivo::removerArquivosDoDiretorio("C:\Diretorio", $extensoes, true);
//Apagar o diretório C:\Diretorio e todos os seus arquivos e subpastas
clsArquivo::removerDiretorio("C:\Diretorio", true);
That's it!
Until later!
Comentários (0)
Carregando comentários…