DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Ribamar has posted 3 posts at DZone. View Full User Profile

Directory List With PHP

03.30.2007
| 16106 views |
  • submit to reddit
        // description of your code here

This code list all files and subdirectories in a dirctory with links.


<html><head><title>ribafs.net - Tutoriais</title></head>
<body bgcolor='#FFFACD'>
<h2 align=center><a href="http://ribafs.net">http://ribafs.net - <?php echo date('d/m/Y H:i:s'); ?></a></h2>

<?php
$dn = opendir (dirname(__FILE__));
$exclude = array("index.php", ".", "..");

// adiciona os arquivos ao array $arquivos
while($fn = readdir($dn)) {
	if ($fn == $exclude[0] || $fn == $exclude[1] || $fn == $exclude[2]) continue;
	$arquivos[] = $fn;
}
// ordena o vetor
sort($arquivos);
// exibe os arquivos

foreach ($arquivos as $arquivo)

if (is_dir($arquivo)){
	$dir .= "<img src='/imagens/diretorio.png'> <a href='$arquivo'>$arquivo</a><br>";
}else{
	$tamanho = filesize($arquivo);
	$m = 'bytes';
	if ($tamanho>1024) {
		$tamanho=round($tamanho/1024,2);
		$m = 'KB';
	} elseif($tamanho > 1024*1024){
		$tamanho = round(($tamanho/1024)/1024,2);
		$m = 'MB';
	}
	$arq .= "<img src='/imagens/arquivo.png'> <a href='$arquivo'>$arquivo</a> - $tamanho $m<br>";
}
echo $dir . $arq;

closedir($dn);
?>