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

displaying folders containing a certain file structure.

I have a directory, which has a lot of subdirectories with very similar content.
the directory structure looks something like this.


directory1
directory1/category1/type1/
directory1/category1/type1/files/
directory1/category1/type1/files/1.jpg
directory1/category1/type1/files/2.jpg
directory1/category1/type1/files/3.jpg
directory1/category1/type1/files/4.jpg


what i would like to do is have a script, which when i put in the directory 1
will scan the directories, and show me this

<p>category1 - type1</p>
<img src="directory1/category1/type1/files/1.jpg"><img src="directory1/category1/type1/files/2.jpg"><img src="directory1/category1/type1/files/3.jpg"><img src="directory1/category1/type1/files/4.jpg">

<br />

<p>category1 - type2</p>
<img src="directory1/category1/type2/files/1.jpg"><img src="directory1/category1/type2/files/2.jpg"><img src="directory1/category1/type2/files/3.jpg"><img src="directory1/category1/type2/files/4.jpg">


etc. I only want the directories listed which contain the files in the files directory named 1.jpg, 2.jpg, 3.jpg and 4.jpg

how would I go about doing this.
thanks for any help.
0
paulp75
Asked:
paulp75
  • 4
  • 3
1 Solution
 
hernst42Commented:
Write a recursive function which loops through the directories given as start parameter. Untested example:

function showPicLinks($dir, $parent)
{
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (is_dir($dir)) {
                 if ($dir[0] != '.') showPicLinks($file, $parent .'/'.$file);
             } else {
                 echo "<img src=\"$parent/$file\">\n";
            }
        }
        closedir($dh);
    }
}
0
 
chingmdCommented:
Writing this a little bit towards the linux/unix side of things.

foreach $dir in (`find . -type d -maxdepth 1`) {
  foreach $dir1 in (`find $dir -type d -maxdepth 1 `){
    foreach $file in (`find $dir/$dir1/files -type -maxdepth 1`) {
      if ($file =~ .jpg&) {
        #output the html code
      }
    }
  }
}

This can be simplified a great deal if you know the directory names before hand.  Then you can just whitelist the names.

If this is on windows, you can use a system call to run a command of dir /ad /b and set that to a array/var name.

0
 
paulp75Author Commented:
hernst,
i tried out your example, and I think it must be pretty close.

it lists the directory category1 in this example with a warings.
Warning: opendir(category1) [function.opendir]: failed to open dir: No such file or directory in **** on line 13.
line 13 is the line with
if ($dh = opendir($dir)) {


it loops through all the directories at that level with the same warning.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
hernst42Commented:
Ok this line
 if ($dir[0] != '.') showPicLinks($file, $parent .'/'.$file);
might be
 if ($dir[0] != '.') showPicLinks($parent.'/'.$file, $parent);
0
 
paulp75Author Commented:
still having the same problem. tried a few things but no luck.
0
 
paulp75Author Commented:
also couldnt get chingmd's example to work either
0
 
hernst42Commented:
So here should be a working example:

<?php

function showPicLinks($dir, $parent)
{
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (is_dir($dir . '/' . $file)) {
                 if ($file[0] == '.') {
                     continue;
                 }
                 showPicLinks($dir .'/'.$file, $parent.'/'.$file);
             } else {
                 echo "<img src=\"$parent/$file\">\n";
            }
        }
        closedir($dh);
    }
}

showPicLinks('p:/public_html/ee/directory1', '.');
0
 
paulp75Author Commented:
still doesnt seem to work, but I did manage to get another method going, which is very similar, so I'll accept your answer there,
thanks for the help.

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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