Views: 404 views
Staff,
Good night!
This time I come to comment on a problem that bothered me a little bit 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 this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
<?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 the listFiles and the extension handle. Therefore, I will focus on the new functions. Using the class is quite simple for our problem:
1 2 3 4 5 6 7 8 9 10 11 12 |
//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!
See you!