Solved

PHP: Recursive FilesystemIterator

Posted on 2016-10-06
2
76 Views
Last Modified: 2016-10-07
How can I make this recursive?  
Now it only finds files in the "courses" directory.  I want it to also look in sub-directories.

$filesKits=array();
$regex = "/[a-zA-Z]+[\)]?+[0-9]+[e]?\.zip$/";
$path = 'files/courses';
$iterator = new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS + FilesystemIterator::CURRENT_AS_PATHNAME + FilesystemIterator::KEY_AS_FILENAME);
foreach ($iterator as $key => $filepath) {
  if (preg_match($regex, $key)) {
    $filesKits[] = $filepath;
  }
}
0
Comment
Question by:skij
2 Comments
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 41832560
Here's an example that you might be able to tailor for your needs.  You would run it with a URL like this (but I am not leaving something like this on my server):
https://iconoun.com/demo/find_files.php?q=/error_log/i
<?php // demo/find_files.php
/**
 * Put this script in the target root directory
 *
 * Traverse all directories down the web tree
 * Show all files that match the ?q={REGEX} URL argument pattern
 *
 * http://php.net/manual/en/class.recursivedirectoryiterator.php#85805
 */
ob_start();
error_reporting(E_ALL);


// START IN THE CURRENT DIRECTORY
$path = realpath(getcwd());

// THE FILE NAME PATTERN
$regex   = $_GET['q'];
if (empty($regex)) trigger_error("Missing q= URL Argument; please specify the REGEX for the file name.", E_USER_ERROR);
$p_regex = htmlentities($regex);

// COLLECT THE DIRECTORY INFORMATION OBJECTS
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);


// ITERATE OVER THE OBJECTS
foreach($objs as $name => $obj)
{
    // PROCESS THE MATCHED FILE NAME(S) ONLY
    if (preg_match($regex, $name))
    {
        echo PHP_EOL . "<br>$name";
    }
}


// IF THERE ARE NO MATCHING FILE(S)
$out = ob_get_clean();
if (empty($out))
{
    echo "<h3>No match for $p_regex found.</h3>";
}
else
{
    echo "<h3>Files matching regex: <i>$p_regex</i></h3>";
}


// SHOW THE GIT BRANCH
$root = '.git/HEAD';
$text = @file_get_contents($root);
if ($text)
{
    $text = explode(DIRECTORY_SEPARATOR, $text);
    $text = array_slice($text, 2);
    $name = implode(DIRECTORY_SEPARATOR, $text);
    echo PHP_EOL . "<br>On Git branch: $name" . PHP_EOL;
}
else
{
    echo PHP_EOL . "<br>On Git branch: UNKNOWN" . PHP_EOL;
}


// REPORT THE FILES
echo $out;

Open in new window

0
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 41832751
<?php
$path = dirname(__FILE__);
$regex = '/[a-zA-Z]+[\)]?+[0-9]+[e]?\.zip$/';
$result = array();
findAll($path, $regex, $result);
echo "<pre>" . print_r($result, true) . "</pre>";

function findAll($path, $pattern, &$result) 
{
  $iterator = new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS + FilesystemIterator::CURRENT_AS_PATHNAME + FilesystemIterator::KEY_AS_FILENAME);
  foreach ($iterator as $key => $filepath) {
    if (is_dir($filepath)) {
      findAll($filepath, $pattern, $result);
    }
    else {
      if (preg_match($pattern, $key)) {
        $result[] = $filepath;
      }
    }
  }
}

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question