Explorateur de repertoires en php

Contenu du snippet

visualise le contenu des (sous)repertoires et les affiche dans une page html

Source / Exemple :


<HTML>
<!-- DATE DE CREATION: <?php print (date("r")); ?> -->
<HEAD>
<TITLE>Explorateur PHP</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<BODY BGCOLOR=#ACD0D7 TEXT=#000000 LINK=#009900 VLINK=#FF6633 ONLOAD="prof.focus()">

<?php 
function getmicrotime(){
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
  }
  $time_start = getmicrotime();
  
$prof=$_POST['prof'];
if (empty($prof)){$prof=1;}
?>
 <FORM ACTION="<?php print $_SERVER['SCRIPT_NAME'];  ?>" METHOD=POST>
<P ALIGN="center">
<B>Profondeur d'exploration </B>
<INPUT TYPE="text" NAME="prof" SIZE=3 MAXLENGTH=3 VALUE="<?php print $prof; ?>" 
  ONCHANGE="window.location.submit(true)">&nbsp;
<INPUT TYPE="button" VALUE="Maxi" ONCLICK="prof.value='max';prof.focus();">
<HR SIZE=3 COLOR="#800000"></P>
<nobr>
<SCRIPT LANGUAGE="PHP">

                                   // ******************************//
                                   //        Initialisation         //
                                   // ******************************//

//$prof="2";                          // Profondeur d'exploration      //
$TotalFichiers=0;                  // Total des fichiers trouver    //
chdir(".");                        // Point de depart d'exploration //

// ***************************           Initialiser la fonction
function Explore($Niv,$path,$prof){
    global $prof,$TotalFichiers;
    $TableRepertoires = array();   //******************************//
    $TableFichiers= array();       //      Variables locale        //
    $Entree= array();              //******************************//
    $dir = opendir(".");
// ***************************             lire les entrees
    while ($File = readdir($dir)){
        if($File != "." && $File != ".."  ) {
        $Entree[]= "$File";
        }
        $i++;
    }
    closedir($dir);
// ***************************              Separer Repertoire / Fichiers
    $Max = count($Entree);
    for ($i = 0 ; $i != $Max ; $i++){
        if (is_dir($Entree[$i])) {
           $TableRepertoires[]="$Entree[$i]";
        }else{
           $TableFichiers[]=  $Entree[$i];
        }
    }
// ***************************              affiche le repertoire
    sort ($TableRepertoires);
    $Max = count($TableRepertoires);
    for($i = 0; $i != $Max; $i++){
        echo str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|",$Niv-1);
        //print "<B> <font color=\"#7F0000\">$Niv/$prof</font> <FONT COLOR=\"#FF0000\">&nbsp;$TableRepertoires[$i]</FONT></B> <BR>\n";
        print "<font color=\"#7F0000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;&nbsp;<b>$Niv/$prof</b></font><font size=\"+1\" color=\"#FF0000\">
                <a href=\"$path$TableRepertoires[$i]/\"><b>$TableRepertoires[$i]</b></a></font><br />\n";
        if ($Niv != $prof) {        // si < maxi profondeur on y retourne
		
           $Niv++;
           $path_bak = $path;
           chdir ("$TableRepertoires[$i]");
           Explore($Niv,"$path$TableRepertoires[$i]/","");
           chdir("..");
           $path = $path_bak;
           $Niv--;
        }
    }
// ***************************              on affiche simplement les fichiers
    sort ($TableFichiers);
    $Max = count($TableFichiers); 
    for($i = 0; $i != $Max; $i++){
        echo str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|",$Niv);
        //print "<FONT COLOR=\"#0000FF\"><B>&nbsp;&nbsp;$TableFichiers[$i]</B></FONT><BR>\n";
        print "&nbsp;&nbsp;<a href=\"$path$TableFichiers[$i]\">$TableFichiers[$i]</a><br />\n";
    }
    $TotalFichiers += $Max;
    echo str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|",$Niv-1);
	print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'-»";
    print "<b><i>&nbsp;&nbsp;$Max</i> Fichiers </b><br>";
    echo str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|",$Niv-1);
	print "<br>";
}
// ***************************             fin des fonctions

$Table=Explore(1,"",$prof); // ********          on execute la fonction
  $time_end = getmicrotime(); // temp final au retour
  $time = $time_end - $time_start; // calcule le temp d'execution du script
print"<b><i><br>&nbsp;$TotalFichiers</i> Fichiers  trouves</b> en: $time secondes<br>";

</SCRIPT>
</FORM>
</nobr>
</BODY>
</HTML>

Conclusion :


- attention une trop grande profondeur d'exploration conduit a un timeout et les
liens symbolique sont des boucles d'exploration possibles donc timeout aussi
- source commenter pour une adaptation possible
- copier le sur votre serveur ,le nom na pas d'importance
en .php .php3 .php4 ...... il marche avec la directive RegisterGlobal = off
- respect les .htaccess (donne l'erreur et continu)

+++ un script tout cru pour ne pas se casser la tronche a le faire +++

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.