Page 1 of 1

[SOLVED] Recurse folders & files

Posted: Sun Oct 31, 2004 12:18 pm
by rhosk
New here. Looks like a great place to get some answers. I won't be long winded.

I have a folder structure that has images in the respective folders. I have a script that recurses these folders fine and prints out a directory tree. My question is: How can I just pull out the thumbnail images (prefixed "thumb_") and print out the same tree with only the thumbnail image links.

Code: Select all

<?php
create_tree(".");
function create_tree($file_dir)
{
        if ($handle = @opendir($file_dir))
        {
                $i=0;
                while (false !== ($file = @readdir($handle))) { 
	                if ($file != "." && $file != "..") { 
                               $list[$i]=$file;
                                $i++;
                        }
                }
                $dir_length = count($list);
                echo "<ul>";
                for($i=0;$i<$dir_length;$i++)
                {
                        if(strrpos($list[$i], ".") == FALSE)
                        {
                                echo "<li>".$list[$i]."/</li>\n";
                                create_tree($file_dir."/".$list[$i]);
                        }
                        else
                        {
                                echo "<li><a href="".$file_dir."/".$list[$i]."">".$list[$i]."</a></li>\n";
                        }
                }
                echo "</ul>";
                closedir($handle);
        }
}
?>
The ultimate goal is to populate a xml file with the paths as an element. Actually to go further, how would I get it to just give me the path filenames and no folder output.

OK, one question at a time. Don't want to get too greedy. The latter is not that important right now. Thanks.

Posted: Sun Oct 31, 2004 1:42 pm
by kettle_drum

Code: Select all

loop_through_each_file{
   if(substr($file_name, 0, 6)=="thumb_"){
      //its a thumbnail
   }
}

Posted: Sun Oct 31, 2004 2:13 pm
by rhosk
OK, I don't mean to sound ignorant, but where and how would I apply your code? I'm not well versed in PHP yet and would appreciate a bit more guidance. I can see how it would work, but I just don't know where to insert it and output it. Thanks for your response, kettle_drum!

Posted: Mon Nov 01, 2004 6:56 am
by rhosk
bump, anyone?

Posted: Mon Nov 01, 2004 7:27 am
by Weirdan
I tweaked you function to accept additional $filter parameter (and made some cleanup). Enjoy.

Code: Select all

create_tree("."); // show all files
create_tree(".", '/^thumb_/'); // guess what 
function create_tree($file_dir, $filter = '') { // added second parameter
        if ($handle = @opendir($file_dir))
        {
                $list = array();
                while (false !== ($file = @readdir($handle))) { 
                    if ($file != "." && $file != "..") {
                               if( !empty($filter) && !is_dir($file_dir . '/' . $file) )  // do not filter out directories
                                    if( !preg_match($filter, $file) )  continue; // skip to the next file
                               $list[] = $file; // no need to use $i var
                        }
                }
                echo "<ul>";
                foreach($list as $file) { // you can iterate through an array like this
                        if( is_dir($file_dir . '/' . $file) ) { // and you may have files 
                                                                // without extensions as well 
                                                                // as directories with 
                                                                // extensions, hence your 
                                                                // check was not reliable
                                echo "<li>" . $file . "/</li>\n";
                                create_tree($file_dir . "/" . $file, $filter); 
                        } else
                                echo "<li><a href="" . $file_dir . "/" . $file . "">" . $file . "</a></li>\n";

                }
                echo "</ul>";
                closedir($handle);
        }
}

Posted: Mon Nov 01, 2004 8:08 am
by rhosk
Weirdan, I thank you kindly for your reply, but this doesn't work. It's traversing through the folders and it's not filtering out the thumb_. It's only giving me the thumbs if it's in the same folder :) (Which helps me as well!).

What would I modify to have it traverse through all the folders?

folder1
----folder2
-------folder3

---------thumb_1.jpg
---------thumb_2.jpg

and so on. Thanks again, I'm learning.

Posted: Mon Nov 01, 2004 8:52 am
by Weirdan
I edited my post (forgot to pass the $filter down the tree). That should work

Posted: Mon Nov 01, 2004 9:13 am
by rhosk
Thanks very much! That did the trick.