et4891 Posted June 5, 2013 Share Posted June 5, 2013 I'm actually thinking of using jquery lightbox to apply to all my images but I figured with jquery lightbox I should at least make a page with all thumbnail images. So I'm trying to think of a way to easily bring out all the images in the folder+subfolders, but somehow I'm searching through sites/goolging most of them only talk about scandir and glob. I tried something like... foreach (glob("*.png") as $filename) { echo "$filename size " . filesize($filename) . "\n"; } but glob still doesn't go INTO sub directories same as for scandir... I can do the same code few times to check each sub directories but it'll be a annoying to do so... $image_files = scandir("./images/"); I thought of doing the code above and sub a variable into ./images/variable within a loop or something but somehow I just can't get outside the box and get it to work... anyone able to give me a hint or give me a hand? Thanks in advance... Quote Link to comment https://forums.phpfreaks.com/topic/278795-how-to-find-all-images-within-subfolders/ Share on other sites More sharing options...
et4891 Posted June 5, 2013 Author Share Posted June 5, 2013 I found a way to do what I want but still missing something here I couldn't figure out... this is what I have at the moment... <?php function dirToArray($dir) { $contents = array(); foreach (scandir($dir) as $node) { if ($node == '.' || $node == '..') continue; if (is_dir($dir . '/' . $node)) { $contents[$node] = dirToArray($dir . '/' . $node); } else { $contents[] = $node; } } return $contents; } $c=(""); $r = dirToArray('./images'); foreach($r as $a) { foreach($a as $b) { if(substr($b, -3) == "png") { $c.="<img src='/images/" . $b . ".png'>"; } } } echo $c; ?> but when I look at the source code....I see... <img src='/images/Screenshot_2013-06-02-19-41-36.png'> but I need it to be <img src='/images/(directory)/Screenshot_2013-06-02-19-41-36.png'> somehow I can't think how to implement the (directory) in... Quote Link to comment https://forums.phpfreaks.com/topic/278795-how-to-find-all-images-within-subfolders/#findComment-1434210 Share on other sites More sharing options...
kicken Posted June 5, 2013 Share Posted June 5, 2013 Are you looking for code that will recurse down into all sub directories (and sub's of them and so on) or are you just wanting to find images from a specific subdirectory? If it's a specific sub directory, just pass it as part of the path, eg: foreach (glob('./images/*.png') as $filename) If you want a recursive setup you can look into RecursiveDirectoryIterator or scandir with a loop and stack. Quote Link to comment https://forums.phpfreaks.com/topic/278795-how-to-find-all-images-within-subfolders/#findComment-1434211 Share on other sites More sharing options...
et4891 Posted June 5, 2013 Author Share Posted June 5, 2013 (edited) Are you looking for code that will recurse down into all sub directories (and sub's of them and so on) or are you just wanting to find images from a specific subdirectory? If it's a specific sub directory, just pass it as part of the path, eg: foreach (glob('./images/*.png') as $filename) If you want a recursive setup you can look into RecursiveDirectoryIterator or scandir with a loop and stack. not a specific sub directory...I want to find all images in all sub directory and then make it thumbnail. Edited June 5, 2013 by et4891 Quote Link to comment https://forums.phpfreaks.com/topic/278795-how-to-find-all-images-within-subfolders/#findComment-1434212 Share on other sites More sharing options...
Solution AbraCadaver Posted June 5, 2013 Solution Share Posted June 5, 2013 I had a better one that used references but this is what I found: function glob_recursive($pattern, $flags = 0) { $files = glob($pattern, $flags); foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags)); } return $files; } Quote Link to comment https://forums.phpfreaks.com/topic/278795-how-to-find-all-images-within-subfolders/#findComment-1434304 Share on other sites More sharing options...
et4891 Posted June 5, 2013 Author Share Posted June 5, 2013 I had a better one that used references but this is what I found: function glob_recursive($pattern, $flags = 0) { $files = glob($pattern, $flags); foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags)); } return $files; } thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/278795-how-to-find-all-images-within-subfolders/#findComment-1434318 Share on other sites More sharing options...
enigmawebdesign Posted January 31, 2016 Share Posted January 31, 2016 Hi Guys,I know it's an old topic but could someone post the complete working code?I need a solution for a customer in which I retrieve images from multiple subfolders.Br,Steve Quote Link to comment https://forums.phpfreaks.com/topic/278795-how-to-find-all-images-within-subfolders/#findComment-1530619 Share on other sites More sharing options...
Jacques1 Posted January 31, 2016 Share Posted January 31, 2016 Create your own thread with a specific problem description instead of hijacking ancient threads from other users. Also note this is a help forum, not a Gimme-the-code forum. Explain what you've tried and where you got stuck (in the new thread). Quote Link to comment https://forums.phpfreaks.com/topic/278795-how-to-find-all-images-within-subfolders/#findComment-1530621 Share on other sites More sharing options...
enigmawebdesign Posted January 31, 2016 Share Posted January 31, 2016 Hi Jacques1,I'm sorry about this and will create a new thread.Cheers,Steve Quote Link to comment https://forums.phpfreaks.com/topic/278795-how-to-find-all-images-within-subfolders/#findComment-1530624 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.