Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

PHP: Recursive FilesystemIterator

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
skij
Asked:
skij
2 Solutions
 
Ray PaseurCommented:
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
 
Julian HansenCommented:
<?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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now