[SOLVED] Recurse folders & files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

[SOLVED] Recurse folders & files

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Code: Select all

loop_through_each_file{
   if(substr($file_name, 0, 6)=="thumb_"){
      //its a thumbnail
   }
}
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

Post 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!
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

Post by rhosk »

bump, anyone?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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);
        }
}
Last edited by Weirdan on Mon Nov 01, 2004 8:52 am, edited 1 time in total.
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I edited my post (forgot to pass the $filter down the tree). That should work
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

Post by rhosk »

Thanks very much! That did the trick.
Post Reply