views:

82

answers:

4

I'd like to make a gallery of all images i have under my domain (my internet root folder). All these images are in different folders. What's the best way to 'browse' through all the folders and return the images?

+1  A: 

Take a look at opendir you would want to write a function that gets called in a recursive loop, the function could loop through the files in the specific directory, check the file extension and return the files as an array which you would merge with a global array.

fire
+1  A: 

Use Google Image Search with site: www.mydomainwithimages.com as the search term and this will show you all your indexed images. This should be everything in your domain as long as your robots.txt file doesn't exclude the Google crawler.

Dave Anderson
Great solution. However i get some strange images from cn.last.fm in my search results also.. Any idea?
Paul
This assumes that every image is linked to from some page that Google indexes.
meagar
A: 

Depends on hosting system, you could use command line with exec or passthru

find /path/to/website/root/ -type f -name '*.jpg'

If you can't do such a thing, as fire said, opendir is the way to go.

Benoit
A: 

I would give PHP's DirectoryIterator a spin.

This is untested pseudo-code, but it should work a little bit like this:

function scanDirectoryForImages($dirPath)
{
    $images = array();
    $dirIter = new DirectoryIterator($dirPath);
    foreach($dirIter as $fileInfo)
    {
        if($fileInfo->isDot()) 
            continue;
        // If it's a directory, scan it recursively
        elseif($fileInfo->isDir())
        {
            $images = array_merge(
                $images, scanDirectoryForImages($fileInfo->getPath())
            );
        }
        elseif($fileInfo->isFile())
        {
            /* This works only for JPEGs, oviously, but feel free to add other
            extensions */
            if(strpos($fileInfo->getFilename(), '.jpg') !== FALSE)
            {
                $images[] = $fileInfo->getPathname();
            }
        }
    }

    return $images;
}

Please don't sue me if this doesn't work, it's really kinda from the top of my hat, but using such a function would be the most elegant way to solve your problem, imho.

// edit: Yeah, that's basically the same as fire pointed out.

Turbotoast