Solved

PHP: Recursive FilesystemIterator

Posted on 2016-10-06
2
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 110

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 58

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

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 i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 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