Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7039759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:50:13+00:00 2026-05-28T01:50:13+00:00

I am working on a pretty large PHP class that does a lot of

  • 0

I am working on a pretty large PHP class that does a lot of stuff with Image Optimization from the Command line, you basically pass the program an Image path or a Folder path that has multiple images inside of it. It then runs the files through up to 5 other command line programs that optimize images.

Below is part of a loop that gathers the images paths, if the path is a Folder instead of an image path, it will iterate over all the images in the folder and add them to the image array.

So far I have everything working for single images and images in 1 folder. I would like to modify this section below so it could recursively go deeper then 1 folder to get the image paths.

Could someone possibly show me how I could modify this below to accomplish this?

// Get files 
if (is_dir($path))
{
    echo 'the path is a directory, grab images in this directory';
    $handle = opendir($path);
    // FIXME : need to run recursively
    while(FALSE !== ($file = readdir($handle))) 
    {
        if(is_dir($path.self::DS.$file))
        {
            continue;
        }
        if( ! self::is_image($path.self::DS.$file))
        {
            continue;
        }
        $files[] = $path.self::DS.$file;
    }
    closedir($handle);



}else{
    echo 'the path is an Image and NOT a directory';
    if(self::is_image($path))
    {   
        echo 'assign image Paths to our image array to process = '. $path. '<br><br>';
        $files[] = $path;
    }
}

if (!count($files))
{
    throw new NoImageFoundException("Image not found : $path");
}

UPDATE

@Chris’s answer got me looking at the Docs and I found an example that I modified to this that seems to work

public static function find_recursive_images($path) {
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),
                                              RecursiveIteratorIterator::CHILD_FIRST);
    foreach ($iterator as $path) {
      if ($path->isDir()) {
         //skip directories
         continue;
      } else {
         $files[] = $path->__toString();
      }
    }
    return $files;
}

…

$files = self::find_recursive_images($path);
echo '<pre>';
print_r($files);
echo '</pre>';
exit();

The output is JUST the filenames and there path like this which is my ultimate goal, so far this works perfect but as always if there is a better way I am all for improving

(
    [0] => E:\Server\_ImageOptimize\img\testfiles\css3-generator.png
    [1] => E:\Server\_ImageOptimize\img\testfiles\css3-please.png
    [2] => E:\Server\_ImageOptimize\img\testfiles\css3-tools-10.png
    [3] => E:\Server\_ImageOptimize\img\testfiles\fb.jpg
    [4] => E:\Server\_ImageOptimize\img\testfiles\mysql.gif
    [5] => E:\Server\_ImageOptimize\img\testfiles\OriginalImages\css3-generator.png
    [6] => E:\Server\_ImageOptimize\img\testfiles\OriginalImages\css3-please.png
    [7] => E:\Server\_ImageOptimize\img\testfiles\OriginalImages\css3-tools-10.png
    [8] => E:\Server\_ImageOptimize\img\testfiles\OriginalImages\fb.jpg
    [9] => E:\Server\_ImageOptimize\img\testfiles\OriginalImages\mysql.gif
    [10] => E:\Server\_ImageOptimize\img\testfiles\OriginalImages\support-browsers.png
    [11] => E:\Server\_ImageOptimize\img\testfiles\support-browsers.png
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:50:14+00:00Added an answer on May 28, 2026 at 1:50 am

    While andreas’ answer probably works, you can also let PHP 5’s RecursiveDirectoryIterator do that work for you and use a more OOP approach.

    Here’s a simple example:

    $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
    
    while ($it->valid())
    {
      if ($it->isDot())
        continue;
    
      $file = $it->current();
      if (self::is_image($file->pathName))
      {
        $files[] = $file->pathName;
      }
    
      $it->next();
    }
    

    Edit:

    Alternatively, you could try this (copied from Zend_Translate_Adapter):

    $it = new RecursiveIteratorIterator(
                    new RecursiveRegexIterator(
                        new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::KEY_AS_PATHNAME),
                        '/^(?!.*(\.svn|\.cvs)).*$/', RecursiveRegexIterator::MATCH
                    ),
                    RecursiveIteratorIterator::SELF_FIRST
                );
    
    foreach ($it as $dir => $info)
    {
      var_dump($dir); 
    }
    

    Cheers

    Chris

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some current projects, I'm working with several data structures that are pretty large
I just started working for a pretty large company and my group manages all
I am working on a large scale project where a custom (pretty good and
I'm working on a large scale performance critical asp web application with a pretty
I've got an app that's working pretty flawlessly in Chrome and FF, however, when
working on a pretty large library of controls the generic.xaml gets simpy out of
I'm working on a iPhone app which has a pretty large UITableView with data
I have a pretty large social network type site I have working on for
Disclaimer: I'm working on Euler Problem 9. I'm adding up some pretty large numbers,
At work we have a pretty large web application that works by having a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.