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
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]
Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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!
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.