Results 1 to 3 of 3

Thread: Reading the directory structure

  1. #1
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading the directory structure

    Hi all,

    I have used the below script to read the directory struture in order to build the tree menu for my web.

    Code:
    <?php
    $sub = ($_GET['dir']);
    $path = 'enter/your/directory/here/';
    $path = $path . "$sub";
    $dh = opendir($path);
    $i=1;
    while (($file = readdir($dh)) !== false) {
        if($file != "." && $file != "..") {
                if (substr($file, -4, -3) =="."){
                echo "$i. $file <br />";
                }else{            
            echo "$i. <a href='?dir=$sub/$file'>$file</a><br />";
              }
            $i++;
        }
    }
    closedir($dh);
    ?>
    This script read the directory structure in alpthabatical order but my requirement is to read on the basis of date created. It will be great if any one could tell me how to achieve it.

    Regards,

    Sameer.

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. #3
    Join Date
    Mar 2009
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hi there,

    I am using the below code while trying to access the directory structure. The code I took from the link above is,

    PHP Code:
    [COLOR="RoyalBlue"]function LoadFiles($dir,$filter="")
    {
     $Files = array();
     $It =  opendir($dir);
     if (! $It)
      die('Cannot list files for ' . $dir);
     while ($Filename = readdir($It))
     {
     if ($Filename != '.' && $Filename != '..'  )
     {
      if(is_dir($dir . $Filename))
       {
       $Files = array_merge($Files, LoadFiles($dir . $Filename.'/'));
       }
     else 
      if ($filter=="" || preg_match( $filter, $Filename ) ) 
       {
       $LastModified = filemtime($dir . $Filename);
       $Files[] = array($dir .$Filename, $LastModified);
       }
        
      else 
       continue;
       
     }
    }
      return $Files;
    }
    function DateCmp($a, $b)
    {
      return  strnatcasecmp($a[1] , $b[1]) ;


    function SortByDate(&$Files)
    {
      usort($Files, 'DateCmp');


    $Files = LoadFiles("../../templates/images/data/communes/");
    SortByDate($Files);
    reset($Files);
    while (list($k,$v) =each($Files))
     {
     ?> - <?=$v[0]?> <?= date('Ymd h:i',$v[1])?></li><?
     
    } [/COLOR]
    where as I am just passing the following $path variable to funcation LoadFiles().

    $sub = ($_GET['dir']);
    $path = 'C:\Inetpub\wwwroot\reports\Backup\Backup Usage\';
    $path = $path."$sub";

    $Files = LoadFiles($path);

    (i also tried plan path i.e $path = 'C:\Inetpub\wwwroot\reports\Backup\Backup Usage\'

    But the program is giving me 'unexpected T_string error'. Does any one got an idea what this error would be reffering to ?

    I will be great if some one could solve this, as it is realy urgent for me.



    Regards,

    Sameer.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •