Recursive File Search (PHP)
You should create a filter:
class JpegOnlyFilter extends RecursiveFilterIterator
{
public function __construct($iterator)
{
parent::__construct($iterator);
}
public function accept()
{
return $this->current()->isFile() && preg_match("/\.jpe?g$/ui", $this->getFilename());
}
public function __toString()
{
return $this->current()->getFilename();
}
}
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
$it = new JpegOnlyFilter($it);
$it = new RecursiveIteratorIterator($it);
foreach ($it as $file)
...
Try this, it uses an array of allowed file types and only echos out the file if the file extension exists within the array.
<?php
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
$allowed=array("pdf","txt");
foreach(new RecursiveIteratorIterator($it) as $file) {
if(in_array(substr($file, strrpos($file, '.') + 1),$allowed)) {
echo $file . "<br/> \n";
}
}
?>
You may also find that you could pass an array of allowed file types to your RecursiveDirectoryIterator
class and only return files that match.
Let PHP do the job:
$directory = new RecursiveDirectoryIterator('path/to/directory/');
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/\.jpe?g$/i', RecursiveRegexIterator::GET_MATCH);
echo '<pre>';
print_r($regex);
<?php
$it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
$display = Array ( 'jpeg', 'jpg' );
foreach(new RecursiveIteratorIterator($it) as $file)
{
if (in_array(strtolower(array_pop(explode('.', $file))), $display))
echo $file . "<br/> \n";
}
?>