开发者

All available images under a domain

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?


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜