Jump to content

Building link array from subfolders


John_A

Recommended Posts

Hi,

I'm throwing together an image gallery script which doesn't use a database, but rather uses a variable passed in the URL (?album=xxx) to display images found in that dir.

I've got the image display and autothumbnial working fine, but I want to add licks to any other galleries (dirs) that exist. The file structure is as follows: -

[pre]
public_html
     |
     |-index.php (the gallery)
     |
     |-[galleries]
            |
            |-[wedding]
            |        |
            |        |-album.txt
            |        |-images (multiple image files)
            |
            |-[holiday]
            |        |
            |        |-album.txt
            |        |-images (multiple image files)
            |
            |-[family]
                     |
                     |-album.txt
                     |-images (multiple image files)
[/pre]

Ideally the album.txt file will be a text file with one line only - the album's description, which is displayed for the links.

So I need some code for index.php which basically: -

1. looks for subfolders of [galleries] and for each one it finds: -
[list]
[*]read the contents of the album.txt file
[*]use this description to display a link, the url would be index.php?album=subdirname
[/list]

Is this possible? And can I do it with a simple one line album.txt file or would that have to be a php file which declares an album title variable?

EDIT: Forgot to say using php4.
Link to comment
https://forums.phpfreaks.com/topic/34423-building-link-array-from-subfolders/
Share on other sites

Okay this is very possible, create a function like this: (This is assuming you are naming your files $dirname.txt
[code]
function get_value($dirname)
{ // Retrieves contents of text file and returns value else returns false

$filename = "/path/to/{$dirname}/{$dirname}.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

if(!empty($contents))
  return $contents;
else
  return false;
}

// This is how you would call the function get the information out of the text file.

$contents = get_value($dirname);
if($contents != false)
  echo $contents;
[/code]
Try this:

[code]
function search_dir($dir,$folders)
{
      if (is_dir($dir)) {
          if ($dh = opendir($dir)) {
              while (($file = readdir($dh)) !== false ) {
                      if( $file != "." && $file != ".." )
                      {
                              if( is_dir( $dir . $file ) )
                              {
                                  $folders[] = $dir . "/" . $file;   
                                  search_dir($dir . $file . "/", $folders);
                              }

                      }
              }
              closedir($dh);
          }
      }
return $folders;
}

$folders = search_dir("path/to/galleries", '');
foreach($folders as $f)
echo $f;
[/code]

WHOOPS! Try this:  ;D
[code]function search_dir($dir,$folders)
{
      if (is_dir($dir)) {
          if ($dh = opendir($dir)) {
              while (($file = readdir($dh)) !== false ) {
                      if( $file != "." && $file != ".." )
                      {
                              if( is_dir( $dir . $file ) )
                              {
                                  $folders[] = $dir . $file;   
                                  search_dir($dir . $file . "/", $folders);
                              }

                      }
              }
              closedir($dh);
          }
      }
return $folders;
}

$folders = search_dir("path/to/galleries", '');
foreach($folders as $f)
echo $f;[/code]

Hmmm, that gave some errors indicating that a path was wrong somewhere. Looking at the rest of the code in the page, it might be duplicating a variable with the code I use for displaying the thumbnails ($dir).

So I took it a step back then used str_replace on $f. Clumsy I know, but it works :)

Thanks again!
I did, but the galleries part was still there, i.e. I got errors about tryint to open galleries/galleries/wedding/album.txt (one too many "galleries" in the link").

Removing it from the path gave me errors elsewhere in the page, and rather than go round in circles I just did my clumsy fix!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.