Jump to content

how to find all images within subfolders


et4891
Go to solution Solved by AbraCadaver,

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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

  • Solution

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

 

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 ^_^

Link to comment
Share on other sites

  • 2 years later...
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.