• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Problem with directory listings showing twice

I have a script which shows thumbnails in all directories, which have the files 1.jpg,2.jpg,3.jpg and 4.jpg in them.

For some reason though, it is showing each directory twice, can anyone see why it would be doing this, i've been looking at this for the past couple hours, but can't work it out.


<?php

 
function getDirectories($directory) {
   // Try to open the directory
   if($dir = opendir($directory)) {
       // Create an array for all directories found
       $tmp = array();
$currentdir = $directory;
       // Add the directories
       while($file = readdir($dir)) {
           // Make sure the directory exists
           if(is_dir($directory . "/" . $file) && $file[0] != '.') {
               array_push($tmp, $directory . "/" . $file);
                     $currentdir = $directory . "/" . $file;
           }
                   // If directory contains the pics, then show them
              if (file_exists($currentdir . "/1.jpg")) {
   ?><h2><?php echo $currentdir ?></h2>
    <img src="<?php echo $currentdir ?>/1.jpg" height="150" ><img src="<?php echo $currentdir ?>/2.jpg" height="150" ><img src="<?php echo $currentdir ?>/3.jpg" height="150" ><img src="<?php echo $currentdir ?>/4.jpg" height="150" ><br>
    <?php
      $currentdir = "";
}
               
       }

       // Finish off the function
       closedir($dir);
       return $tmp;
   }
}

function recurse_directories($directory) {
  $rresults = getDirectories($directory);
  $results = array();

  foreach ($rresults as $result) {
    $more = recurse_directories($result);
    $results[$result] = $more;
  }
  return $results;
}

$startin = "startdirectory/";
$directories = recurse_directories($startin);

?>


thanks
0
paulp75
Asked:
paulp75
1 Solution
 
hernst42Commented:
That's because you don't clear you directory stack properly.
Try the lines:
            // If directory contains the pics, then show them
            if (file_exists($currentdir . "/1.jpg")) {
                ?><h2><?php echo $currentdir ?></h2><img src="<?php echo $currentdir ?>/1.jpg" height="150" ><img src="<?php echo $currentdir ?>/2.jpg" height="150" ><img src="<?php echo $currentdir ?>/3.jpg" height="150" ><img src="<?php echo $currentdir ?>/4.jpg" height="150" ><br>
                <?php
                array_pop($tmp);
                $currentdir = "";
            }
instead of
            // If directory contains the pics, then show them
            if (file_exists($currentdir . "/1.jpg")) {
                ?><h2><?php echo $currentdir ?></h2><img src="<?php echo $currentdir ?>/1.jpg" height="150" ><img src="<?php echo $currentdir ?>/2.jpg" height="150" ><img src="<?php echo $currentdir ?>/3.jpg" height="150" ><img src="<?php echo $currentdir ?>/4.jpg" height="150" ><br>
                <?php
                array_pop($tmp);
                $currentdir = "";
            }

You need to remove the currentdir from $tmp if you found pics to show, else the dir will be processed again.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now