Ir para conteúdo
Fórum Script Brasil
  • 0

Listando arquivos de uma pasta/Diretorio


Abraão Neves

Pergunta

Boa Tarde amigos!

Estou com uma dúvida. Gostaria de listar os arquivos de uma pasta com php. Conseguir o código. Só que queria listar por data de upload. E aí que tá o problema, porque não estou conseguindo e tentei com as dicas vistas em posts de alguns forums e não surtiu efeito. abaixo tenho o codigo.

<html>
<head>
<style type="text/css">

body {
    margin-left: 20px;
    margin-right: 20px;
    color: #333333;
    font-family: arial;
}

a:link {color: #023f88; font-weight: bold; text-decoration: none;}
a:hover {color: #00aeef; font-weight: bold; text-decoration: none;}
a:active {color: #00aeef; font-weight: bold; text-decoration: none;}
a:visited {font-weight: bold; text-decoration: none;}

#pasta {font-size: 20px; color: #023f88; font-weight: normal; text-decoration: none;}
#pasta a:hover {color: #0099ff;}

</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body vlink="#023f88">

<div id="pasta" style="margin-top: 20px; margin-left: 50px;">
<font color="#666666"><strong>Pasta:</strong></font>
<?php
$raiz = end(explode("/", $_SERVER['DOCUMENT_ROOT']));

$pasta = explode("/", $_SERVER['PHP_SELF']);
$tot = count($pasta); $tot--;

if ($tot > "1") {
    echo "<a id=\"pasta\" href=\"";
    for ($z = 0; $z <= $tot; $z++) { echo "../"; }
    echo "\">";
} else { echo "<font color=\"#0066cc\">"; }
   echo $raiz . "/</a></font>";

for ($i = 1; $i <= $tot; $i++) {
    if (@$pasta[$i+1]) {
        if (@$pasta[$i+2]) {
            echo "<a style=\"font-weight: normal;\" href=\"";
            for ($z = 1; $z <= $i; $z++) { echo "../"; }
            echo "\">";
        } else { echo "<font color=\"#0066cc\">";}
        echo $pasta[$i] . "/</a></font>"; $tem = 1;
    }
}
?>
</div>

<div style="margin-top: 20px;">

<?php
// Abre um diretorio conhecido, e faz a leitura de seu conteudo
date_default_timezone_set('America/Sao_Paulo');
$dir = ".";

if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
        if ($file == '..') {
            if (@$tem == 1) {
                echo "<a href=\"" . $file . "\"><img src=\"http://localhost/icons/back.gif\" border=\"0\" /> ";
                echo "Diretório anterior</a><br />";
            } else {
                echo "<br style=\"line-height: 26px;\" />";
            }
            echo "<hr noshade color=\"#cccccc\" style=\"margin-left: -20px;\" />";
        }
        if (is_dir($file) && ($file != ".") && ($file != "..")) {
            echo "<a href=\"" . $file . "\"><img src=\"http://localhost/icons/folder.gif\" border=\"0\" /> $file</a><br />";
        }
    }
    closedir($dh);
}


?>
<table border="0" cellpadding="0" cellspacing="0">
<?php
if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
        $quebra = explode('.', $file);
        $ext = strtolower(end($quebra));

        if (($file != '.') && ($file != '..') && ($ext != $quebra[0]) && ($ext != false)) {
            echo "<tr><td><a href=\"$dir/$file\">";

            if ($quebra[0] == "index") {
                echo "<img src=\"http://localhost/icons/index.gif\" border=\"0\" /> ";
            }
            else if ($ext == "exe" || $ext == "msi") {
                echo "<img src=\"http://localhost/icons/comp.gray.gif\" border=\"0\" /> ";
            }
            else if ($ext == "php" || $ext == "asp" || $ext == "htm" || $ext == "html" || $ext == "shtml" || $ext == "phtml") {
                echo "<img src=\"http://localhost/icons/layout.gif\" border=\"0\" /> ";
            }
            else if ($ext == "jpg" || $ext == "jpeg" || $ext == "gif" || $ext == "png") {
                echo "<img src=\"http://localhost/icons/image2.gif\" border=\"0\" /> ";
            }
            else if ($ext == "js" || $ext == "cgi") {
                echo "<img src=\"http://localhost/icons/script.gif\" border=\"0\" /> ";
            }
            else if ($ext == "mp3" || $ext == "asf" || $ext == "au" || $ext == "wav" || $ext == "mid") {
                echo "<img src=\"http://localhost/icons/sound1.gif\" border=\"0\" /> ";
            }
            else if ($ext == "mpg" || $ext == "mpeg" || $ext == "qt" || $ext == "wmv" || $ext == "mov" || $ext == "avi") {
                echo "<img src=\"http://localhost/icons/movie.gif\" border=\"0\" /> ";
            }
            else if ($ext == "doc" || $ext == "txt" || $ext == "pdf") {
                echo "<img src=\"http://localhost/icons/text.gif\" border=\"0\" /> ";
            }
            else if ($ext == "zip" || $ext == "tar" || $ext == "arj") {
                echo "<img src=\"http://localhost/icons/compressed.gif\" border=\"0\" /> ";
            } else {
                echo "<img src=\"http://localhost/icons/generic.gif\" border=\"0\" /> ";
            }

            echo "" . $file . " </a></td><td align=\"right\" style=\"padding-left: 50px; padding-right: 5px;\">";
            $tamanho = filesize($file);
            if ($tamanho < "1024") {
                echo number_format($tamanho, 0, ",", ".") . " </td><td> bytes </td></tr>";
            }
            else if ($tamanho/1024 < "1024") {
                echo number_format($tamanho/1024, 2, ",", ".") . " </td><td> KB </td></tr>";
            } else {
                echo number_format($tamanho/(1024*1024), 2, ",", ".") . " </td><td> MB </td></tr>";
            }
        }
    }
    closedir($dh);
}
?>

</table>
</div>

</body>
</html>

Link para o comentário
Compartilhar em outros sites

2 respostass a esta questão

Posts Recomendados

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152k
    • Posts
      651,7k
×
×
  • Criar Novo...