Script esplora risorse

atonyc

Utente Attivo
10 Dic 2008
67
0
0
Ciao ragazzi dovrei portare delle piccole modificeh a questo script ma il codice e veramente troppo complesso per me, lo script è praticamente un esplora risorse parte da una directory principale (da me chiamata archivio) per passare alle varie sotto directory e i file in esse contenuti.

Dovrei inserigli queste due particolarità
1) Tutte le varie sottodirectory e file devono apparire in ordine alfabedico e non di creazione (ora se ci fate case mette in alto quella con data di creazione più vecchia per poi proseguire con le altre)
2) Dovrei inserire dei segnalibri ai collegamenti ipertestuali, se ci fate casa se cliccate su una qualsiasi directori lo script si riposizione in alto alla pagina, mentre io vorrei che si posizionasse nel punto in cui clicco.

Spero sia tutto chiaro e vi ringrazio per l'aiuto in anticipo.


PHP:
<?php

function getlastfile($dir)
 {
   $dirarr[]="";
   if(!($dp = opendir($dir)))
    die("woops");
    while($file = readdir($dp))
      if($file != '.' && $file != '..')     
        array_push($dirarr,$file);
   $size=sizeof($dirarr)-1;
 return $dirarr[$size];
 }

// Note to self nodisp variable is simply for looks it serves no real purpose in the function 
// it is false if the last directory is the currenty directory being traversed
function traverse_dir($dirp,$dir_array,$nodisp)
{
  global $PHP_SELF;
  static $loop=1;
  $size = (sizeof($dir_array) - 1);
  $d=rawurlencode(implode("*",$dir_array));
  $dirp .= "/" . $dir_array[$loop];
  $Ivar = "images/tree_split.gif";
  if($loop < $size)
   $dircheck = $loop +1;
  else 
   $dircheck = $loop;
  

 $var2 ="";

  for($iloop=0; $iloop < $loop; $iloop++)
     $var2 .="<img src='images/tree_vertline.gif'>";
    if($nodisp == 0)
      $var2 = substr($var2,36) . " &nbsp&nbsp&nbsp";  
 
  if(!($dp = opendir($dirp)))
    die("Sorry, Page cannot be displayed "); //<script>history.go(-1)</script>");

while($file = readdir($dp))
      if($file != '.' && $file != '..')     
       {
         
         if($lastfile == $file)
             $Ivar = "images/tree_end.gif";
         if($lastfile == $file && $lastfile == $dir_array[$size])
             $nodisp = 0;
         $files = filesize("$dirp/$file");
         $filep = rawurlencode($file);
         // This checks to see if there are anymore sub directories in the dir_array that have not yet been traversed  
          if(is_dir("$dirp/$file") &&  ("$dirp/$dir_array[$dircheck]" == "$dirp/$file") )
            { 
              echo "<tr>$var2<img src=$Ivar><a class=top href='$PHP_SELF?closed=1&fold=$filep&d=$d'><img border=0 src='images\folderopen.gif'></a><a class=top href='$PHP_SELF?closed=1&fold=$filep&d=$d'><b>$file</b></a></tr>";
               $loop++;
                traverse_dir($dirp,$dir_array,$nodisp);
            }
          else if(is_dir("$dirp/$file"))
            echo "<tr>$var2<img src=$Ivar><a class=top href='$PHP_SELF?opened=1&fold=$filep&d=$d'><img border=0 src='images\folder.gif'></a><a class=top href='$PHP_SELF?opened=1&fold=$filep&d=$d'><b>$file</b></a></tr>";
          else
            echo "<tr>$var2<img src=$Ivar><a class=top target=_blank href='$dirp/$file'><img border=0 src='images\file.gif'></a><a class=top target=_blank href='$dirp/$file'>$file</a></tr>"; 

   
 
      }

  $loop++;
 return 0;
}


?>
<?php
if(!isset($fold))
 $fold="";
?>

 
   
<?php
/******************** Define Variables for main code ****************************/
 // Declare some variables that are going to be needed later on


  $basedir="Archivio";   
   if(!isset($d))
    $d="";
   else
    {
      $d = rawurldecode($d);
      $dir_array2 = explode("*",$d);
    }
   if(!isset($dir_array2))
     $dir_array2[0] = $basedir;

    $dir_array = $dir_array2;
    $dir = $dir_array[0]; 

   if(!isset($opened))
     $opened = 0;
   else
     $opened = 1;
   if(!isset($closed))
     $closed = 0;
   else
     $closed = 1;
   
   if($basedir != $dir_array[0])
      die("Sorry, Page cannot be displayed "); //<script>history.go(-1)</script>");
  

/*********************************************************************************/
 // That should be good enough next define what to do if a directory has been opened or closed

   if($closed == 1)
    {
      $fold = rawurldecode($fold);      
      $size2 = sizeof($dir_array)-1;
       while($dir_array[$size2] != $fold)
        {
         array_pop($dir_array); 
         $size2--;
        }
       array_pop($dir_array);
      $opened =1;
    }     

   if($opened == 1)
    {
     if($closed != 1)
      {
       $fold = rawurldecode($fold);
       $dir_array[] = $fold;
      }
 // Above we just added the new folder on and we are going to check and see if it the array still makes a vaild directory. If it doesn't - unload the array back to the base directory and add the new folder on again.
   
        $tmp_dir = implode("/" , $dir_array);
        if(!($tmp = @opendir($tmp_dir)))
          {
           $size2 = sizeof($dir_array) - 1;
             while(!($tmp = @opendir($tmp_dir)))
               {
                 array_pop($dir_array);
                 array_pop($dir_array);
                  $dir_array[] = $fold;
                  $tmp_dir = implode("/" , $dir_array);
               }
          }
     }

/*********************************************************************************************/
?>
<tr>
	<td>&nbsp;</td>
</tr>
<table  width="100%" >
 
    <?php
/*********************************************************************************************/




// Thats it for the opened and closed code we need now were going to execute the initial directory traversion on the base directory.


if(sizeof($dir_array)-1 == 0)
 $size = 0;
else
 $size = 1;

  if(!($dp = opendir($dir)))
    die("Sorry, Page cannot be displayed "); //<script>history.go(-1)</script>");
  


  $d=rawurlencode(implode("*",$dir_array));
  $Ivar = "images/tree_split.gif";
  echo "<tr><a href='$PHP_SELF'><img border=0 src='images/drive.gif'></a><b><a class=top href='$PHP_SELF'></a></b></tr>";
    while($file = readdir($dp))
      if($file != '.' && $file != '..')     
        { 
          $files = filesize("$dir/$file");
          $filep = rawurlencode($file);
           if($lastfile == $file)
             $Ivar = "images/tree_end.gif";
           if( (is_dir("$dir/$file")) && ("$dir/$dir_array[$size]" == "$dir/$file") )
             {
               echo "<tr><img src=$Ivar><a class=top href='$PHP_SELF?closed=1&fold=$filep&d=$d'><img border=0 src='images\folderopen.gif'></a><b><a class=top href='$PHP_SELF?closed=1&fold=$filep&d=$d'>$file</a></b></tr>";
                traverse_dir($dir,$dir_array,1);
              }
             
           else if(is_dir("$dir/$file"))
             echo "<tr><img src=$Ivar><a class=top href='$PHP_SELF?opened=1&fold=$filep&d=$d'><img border=0 src='images\folder.gif'></a><b><a class=top href='$PHP_SELF?opened=1&fold=$filep&d=$d'>$file</a></b></tr>";            
           else
             echo "<tr><img src=$Ivar><a class=top target=_blank href='$basedir/$file'><img border=0 src='images\file.gif'></a><a class=top target=_blank href='$basedir/$file'>$file</a></tr>";
         }


?>
 
Niento ho provato in tutti i modi, ci sono riuscito parzialemente, ovvero mi viene ordinato tutto alfabeticamente tranne le directory che si aprono cliccando sulla directory madre...

Help Help Help
 
Sono riuscito a inserire i segnalibri nei collegamenti ipertestuali, ma per quanto riguarda l'ordinamento alfabetico proprio non riesco ( se qualche anima pia mi darebbe una mana ah ah)

Cmq posto il codice modificato nel caso fosse utile a qualcuno

PHP:
<?php

function getlastfile($dir)
 {
   $dirarr[]="";
   if(!($dp = opendir($dir)))
    die("woops");
    while($file = readdir($dp))
      if($file != '.' && $file != '..')     
        array_push($dirarr,$file);
   $size=sizeof($dirarr)-1;
 return $dirarr[$size];
 }

// Note to self nodisp variable is simply for looks it serves no real purpose in the function 
// it is false if the last directory is the currenty directory being traversed
function traverse_dir($dirp,$dir_array,$nodisp)
{
  global $PHP_SELF;
  static $loop=1;
  $size = (sizeof($dir_array) - 1);
  $d=rawurlencode(implode("*",$dir_array));
  $dirp .= "/" . $dir_array[$loop];
  $Ivar = "images/tree_split.gif";
  if($loop < $size)
   $dircheck = $loop +1;
  else 
   $dircheck = $loop;
  

 $var2 ="";

  for($iloop=0; $iloop < $loop; $iloop++)
     $var2 .="<img src='images/tree_vertline.gif'>";
    if($nodisp == 0)
      $var2 = substr($var2,36) . " &nbsp&nbsp&nbsp";  
 
  if(!($dp = opendir($dirp)))
    die("Sorry, Page cannot be displayed "); //<script>history.go(-1)</script>");
  $lastfile = getlastfile($dirp);
   while($file = readdir($dp))
      if($file != '.' && $file != '..')     
       {
         
         if($lastfile == $file)
             $Ivar = "images/tree_end.gif";
         if($lastfile == $file && $lastfile == $dir_array[$size])
             $nodisp = 0;
         $files = filesize("$dirp/$file");
         $filep = rawurlencode($file);
         // This checks to see if there are anymore sub directories in the dir_array that have not yet been traversed  
          if(is_dir("$dirp/$file") &&  ("$dirp/$dir_array[$dircheck]" == "$dirp/$file") )
            { 
              echo "<tr>$var2<img src=$Ivar><a name='$dirp/$file' class=top href='$PHP_SELF?closed=1&fold=$filep&d=$d#$dirp/$file'><img border=0 src='images\folderopen.gif'></a><a name='$dirp/$file' class=top href='$PHP_SELF?closed=1&fold=$filep&d=$d#$dirp/$file'><b>$file</b></a></tr>";
              $loop++;
                traverse_dir($dirp,$dir_array,$nodisp); 
            }
          else if(is_dir("$dirp/$file"))
            echo "<tr>$var2<img src=$Ivar><a name='$dirp/$file' class=top href='$PHP_SELF?opened=1&fold=$filep&d=$d#$dirp/$file'><img border=0 src='images\folder.gif'></a><a name='$dirp/$file' class=top href='$PHP_SELF?opened=1&fold=$filep&d=$d#$dirp/$file'><b>$file</b></a></tr>";
          else
            echo "<tr>$var2<img src=$Ivar><a class=top target=_blank href='$dirp/$file'><img border=0 src='images\file.gif'></a><a class=top target=_blank href='$dirp/$file'>$file</a></tr>"; 
 
      }

  $loop++;
 return 0;
}


?>
<?php
if(!isset($fold))
 $fold="";
?>

 
   
<?php
/******************** Define Variables for main code ****************************/
 // Declare some variables that are going to be needed later on


  $basedir="Archivio";   
   if(!isset($d))
    $d="";
   else
    {
      $d = rawurldecode($d);
      $dir_array2 = explode("*",$d);
    }
   if(!isset($dir_array2))
     $dir_array2[0] = $basedir;

    $dir_array = $dir_array2;
    $dir = $dir_array[0]; 

   if(!isset($opened))
     $opened = 0;
   else
     $opened = 1;
   if(!isset($closed))
     $closed = 0;
   else
     $closed = 1;
   
   if($basedir != $dir_array[0])
      die("Sorry, Page cannot be displayed "); //<script>history.go(-1)</script>");
  

/*********************************************************************************/
 // That should be good enough next define what to do if a directory has been opened or closed

   if($closed == 1)
    {
      $fold = rawurldecode($fold);      
      $size2 = sizeof($dir_array)-1;
       while($dir_array[$size2] != $fold)
        {
         array_pop($dir_array); 
         $size2--;
        }
       array_pop($dir_array);
      $opened =1;
    }     

   if($opened == 1)
    {
     if($closed != 1)
      {
       $fold = rawurldecode($fold);
       $dir_array[] = $fold;
      }
 // Above we just added the new folder on and we are going to check and see if it the array still makes a vaild directory. If it doesn't - unload the array back to the base directory and add the new folder on again.
   
        $tmp_dir = implode("/" , $dir_array);
        if(!($tmp = @opendir($tmp_dir)))
          {
           $size2 = sizeof($dir_array) - 1;
             while(!($tmp = @opendir($tmp_dir)))
               {
                 array_pop($dir_array);
                 array_pop($dir_array);
                  $dir_array[] = $fold;
                  $tmp_dir = implode("/" , $dir_array);
               }
          }
     }

/*********************************************************************************************/
?>
<tr>
	<td>&nbsp;</td>
</tr>
<table  width="100%" >
 
    <?php
/*********************************************************************************************/




// Thats it for the opened and closed code we need now were going to execute the initial directory traversion on the base directory.


if(sizeof($dir_array)-1 == 0)
 $size = 0;
else
 $size = 1;

  if(!($dp = opendir($dir)))
    die("Sorry, Page cannot be displayed "); //<script>history.go(-1)</script>");
  $lastfile = getlastfile($dir); 
  $d=rawurlencode(implode("*",$dir_array));
  $Ivar = "images/tree_split.gif";
  echo "<tr><a href='$PHP_SELF'><img border=0 src='images/drive.gif'></a><b><a class=top href='$PHP_SELF'></a></b></tr>";
    while($file = readdir($dp))
      if($file != '.' && $file != '..')     
        { 
          $files = filesize("$dir/$file");
          $filep = rawurlencode($file);
           if($lastfile == $file)
             $Ivar = "images/tree_end.gif";
           if( (is_dir("$dir/$file")) && ("$dir/$dir_array[$size]" == "$dir/$file") )
             {
               echo "<tr><img src=$Ivar><a class=top href='$PHP_SELF?closed=1&fold=$filep&d=$d'><img border=0 src='images\folderopen.gif'></a><b><a class=top href='$PHP_SELF?closed=1&fold=$filep&d=$d'>$file</a></b></tr>";
                traverse_dir($dir,$dir_array,1);
              }
             
           else if(is_dir("$dir/$file"))
             echo "<tr><img src=$Ivar><a class=top href='$PHP_SELF?opened=1&fold=$filep&d=$d'><img border=0 src='images\folder.gif'></a><b><a class=top href='$PHP_SELF?opened=1&fold=$filep&d=$d'>$file</a></b></tr>";            
           else
             echo "<tr><img src=$Ivar><a class=top target=_blank href='$basedir/$file'><img border=0 src='images\file.gif'></a><a class=top target=_blank href='$basedir/$file'>$file</a></tr>";
         }


?>
 
so che dovrei iserire la funzione natsort ma proprio nn riesco a capire dove inserirla
 

Discussioni simili