Skip to content

Instantly share code, notes, and snippets.

@rduarte
Created January 9, 2009 18:21
Show Gist options
  • Save rduarte/45209 to your computer and use it in GitHub Desktop.
Save rduarte/45209 to your computer and use it in GitHub Desktop.
Considere a seguinte estrutura de arquivos:
./Carros/
./Carros/1.0/ka.txt
./Carros/1.0/clio.txt
./Carros/1.0/uno.txt
./Carros/1.8/palio.txt
./Carros/2.2/vectra.txt
./Motos/monareta.txt
./Motos/100/biz.txt
./Motos/125/biz.txt
./Motos/150/
./Motos/250/fazer.txt
Crie uma função, que receba por parâmetro o caminho do diretório a ser
analisado, e popule um array multi-dimensional (matriz) com os diretórios
que possuem algum arquivo. A função deve retornar um array quando existirem
arquivos, 'null' quando o diretório solicitado estiver vazio e 'false' se
acontecer algum problema externo('sem permissão', 'não encontrado', etc).
Exiba o resultado em listas ordenadas(<ol>) semanticamente.
<?php
function buscarArvoreDiretorio( $outerDir = './'){
if($dirs = @scandir($outerDir )){
$dirs = array_diff($dirs, array( ".", ".." ));
$dir_array = array();
if(count($dirs) > 0){
foreach( $dirs as $d ){
if( is_dir($outerDir."/".$d) ) $dir_array[$d] = buscarArvoreDiretorio( $outerDir."/".$d );
else $dir_array[$d] = $d;
}
return $dir_array;
}else{
return null;
}
}else{
return false;
}
}
function mostrarArvoreDiretorio($arr){
if(is_array($arr)){
foreach($arr as $key=>$value){
echo "\t<li>".$key;
if(is_array($arr[$key])){
echo "\n\t\t<ol>\n";
mostrarArvoreDiretorio($arr[$key]);
echo "\t\t</ol>\n";
}
echo "\t</li>\n";
}
}else{
return false;
}
}
$tree = buscarArvoreDiretorio('./');
?>
<ol>
<? mostrarArvoreDiretorio($tree); ?>
</ol>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment