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

Exibir Conteúdo De Uma Pasta


Beraldo

Pergunta

Script postado por: Illidan

Exibir conteúdo de uma pasta

Criei um script que exibe o conteúdo de uma pasta do servidor de forma mais limpa e organizada. Na exibição padrão, ele mistura tudo (diretórios, arquivos), além de quebrar ao meio o nome de alguns arquivos. Assim fica bem melhor de navegar pelas pastas do seu servidor =)

Salve como "index.php". Mas tome cuidado pra não substituir um index já existente na pasta!

<?php

/*********************************************\
#   Script que exibe o conteúdo de uma        #
#   pasta de forma mais organizada. Salve     #
#   como "index.php", mas não se esqueça de   #
#   verificar se já existe um arquivo com     #
#   esse nome na pasta.                       #
#                                             #
#   Autor: Carlos H. Reche                    #
#   E-mail: carlosreche@yahoo.com             #
#                                             #
#   Por favor, mantenha os créditos =)        #
#                                             #
\*********************************************/

?>
<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>
</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
$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

6 respostass a esta questão

Posts Recomendados

  • 0

Caso eu queira mudar o valor de $dir, porque ele está recebendo o valor "." que é o diretório principal, como eu faço?

porque aqui ele estava "." que seria "www" so que não queria que ele listasse essa pasta e sim outra "fotos" que esta logo a frente da www, www > fotos, so que quando coloco "./fotos/" da erro.

Link para o comentário
Compartilhar em outros sites

  • 0

Meu script é parecido com esse, so que o meu so lista diretorios e não lista arquivos, mas para fazer um teste usei esse script e deu u mesmo erro. quando o $dir tem valor "." ele lista normalmente, mas quando muda valor ele n lista. quando coloquei "galeria/" ele não listo nada e deu a msg "No folder", mas existe sim uma pasta la.

Link para o comentário
Compartilhar em outros sites

  • 0

Oo

<?php
// Par défaut : anglais
$directoryListing = "Folders in server root (\"www\")";
$noDir = "no folder";
$presentation = "This page display folders in server root. You can delete this page if you want to organize differently the folder. There'e a backup oh this file in &quot;safe&quot; directory (index-safe.php).";

$browser_languages = explode(",", getenv("HTTP_ACCEPT_LANGUAGE"));
$nb_browser_languages = sizeof($browser_languages);
$browser_lang = "";    // ne pas initialiser
$biContinue = true;

for ($niI = 0; $biContinue==true && $niI < $nb_browser_languages; $niI++)
{
    $biContinue = false;
    $lg = explode("-", $browser_languages[$niI]);
    switch ($lg[0])
    {
    case "fr" : $directoryListing = "R&eacute;pertoire(s) &agrave; la racine du serveur (\"www\")";
                $noDir = "aucun répertoire";
                $presentation = "Cette page permet de visualiser les r&eacute;pertoires plac&eacute;s &agrave; la racine du serveur. Si vous souhaitez organiser autrement le r&eacute;pertoire &quot;www&quot;, vous pouvez effacer ce fichier. Il en existe une copie de sauvegarde dans le r&eacute;pertoire &quot;safe&quot; (index-safe.php).";
                break;
    case "en" : break;
    default: $biContinue = true;
    }
}
                
?>
<html>
<head>
<title>[EasyPHP] - Web Local</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.text1 {font-family: Arial, Helvetica, sans-serif;font-size: 12px;color: White;text-align : left;}
.text2 {font-family: Arial, Helvetica, sans-serif;font-size: 12px;color: Silver;text-align : left;}
.titre1 {font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: bold;color: #FFFFFF;}
.titre2 {font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: bold;color: #cccccc;}
</style>
</head>

<body bgcolor="#525A73">

<div align="center">
<table width="400" cellspacing="0" cellpadding="0" border="0" align="center">
<tr>
<td>
<a href="../index.php"><img src="../images_easyphp/titre_easyphp_weblocal.gif" width="387" height="116" border="0"></a>
</td>
</tr>
<tr>
</table>
</div>

<table width="500" border="0" cellspacing="4" cellpadding="0" align="center">
<tr> 
<td><img src="../images_easyphp/cube_rouge_small.gif" width="18" height="20"></td>
<td width="100%" nowrap class="titre1">&nbsp;<? echo $directoryListing; ?>&nbsp;:</td>
</tr>
</table>

<table width="500" border="0" cellspacing="2" cellpadding="0" align="center">
<?
$diretorio = 'galeria/';
$rep=opendir($diretorio);
$bAuMoinsUnRepertoire = false;
while ($file = readdir($rep)){
    if($file != '..' && $file !='.' && $file !=''){ 
        if (is_dir($file)){
            $bAuMoinsUnRepertoire = true;
            print("<tr><td nowrap class='text1'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>");
            print("<td width='100%' class='text1'>");
            print("&nbsp;&nbsp;<img src='images_easyphp/dossier.gif' width='23' height='16' align='absbottom'>&nbsp;");
            print("<a href='$file/' class='text1'>$file</a>");
            print("</td></tr>");
        }
    }
}
if ($bAuMoinsUnRepertoire == false) {
    print("<tr><td nowrap class='text1'><div align='center'>-&nbsp; $noDir &nbsp;-</div></td>");
    print("</td></tr>");
}

closedir($rep);
clearstatcache();
?>
</table>

<br>

<table width="500" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td class="text1">
<? echo $presentation ?> 
</td>
</tr>
</table>
</body>
</html>

Se o seu so lista pastas, poderia me passar?

Editado por JapZ
Link para o comentário
Compartilhar em outros sites

  • 0

Axo que intendie o que o colega esta, ou estava, querendo...

Esse codigo funcionou legal, mais só se colocasse como index

mais não queria assim, porque iria mostrar tambem o index

ai botei a pagina em outro lugar...

Fiz assim:

<?php

/*********************************************\

# Script que exibe o conteúdo de uma #

# pasta de forma mais organizada. Salve #

# como "index.php", mas não se esqueça de #

# verificar se já existe um arquivo com #

# esse nome na pasta. #

# #

# Autor: Carlos H. Reche #

# E-mail: carlosreche@yahoo.com #

# #

# Por favor, mantenha os créditos =) #

# #

\*********************************************/

?>

<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>

</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

$dir = "aqui botei o nome da pasta";

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;\">";

// Aqui em baixo estava dando um erro na hora de mostrar o tamanho dos arquivos

// então coloquei assim

$tamanho = filesize("/home/www/emerson.eu.pn/fotos/$file");

// OBS: esse caminho muda em alguns servers

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

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,8k
×
×
  • Criar Novo...