Gather image file paths Recursively in PHP

╄→尐↘猪︶ㄣ 提交于 2020-01-02 05:31:12

问题


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:


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




回答2:


Create a recursive function to read a directory, then read further if a directory is found during the loop.

Something along the line of:

function r_readdir($path) {
   static $files = array();
   if(!is_dir($path)) {
      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;
      }
   } else {
      while(FALSE !== ($file = readdir($handle))) 
      {
         if(is_dir($path.self::DS.$file))
         {
            r_readdir($path.self::DS.$file);
         }
         if( ! self::is_image($path.self::DS.$file))
         {
            continue;
         }
      }
      closedir($handle);
   }
   return $files;
}


来源:https://stackoverflow.com/questions/8799774/gather-image-file-paths-recursively-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!