John_A Posted January 16, 2007 Share Posted January 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
makeshift_theory Posted January 16, 2007 Share Posted January 16, 2007 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] Quote Link to comment Share on other sites More sharing options...
John_A Posted January 16, 2007 Author Share Posted January 16, 2007 Thanks very much!Unfortunately I've no idea how to build the link array from subdirs found (of [galleries]), any pointers? Quote Link to comment Share on other sites More sharing options...
makeshift_theory Posted January 16, 2007 Share Posted January 16, 2007 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] Quote Link to comment Share on other sites More sharing options...
John_A Posted January 16, 2007 Author Share Posted January 16, 2007 Thanks!That returns: -galleries//wedding galleries//holidaygalleries//familyHow would I strip the galleries// from the beginning of each? Quote Link to comment Share on other sites More sharing options...
makeshift_theory Posted January 16, 2007 Share Posted January 16, 2007 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] Quote Link to comment Share on other sites More sharing options...
John_A Posted January 16, 2007 Author Share Posted January 16, 2007 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! Quote Link to comment Share on other sites More sharing options...
makeshift_theory Posted January 16, 2007 Share Posted January 16, 2007 You would have to change the path to your folder, my default path is still there. Quote Link to comment Share on other sites More sharing options...
John_A Posted January 16, 2007 Author Share Posted January 16, 2007 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.