Reputation: 1439
I'm using something like:
class MyRecursiveFilterIterator extends RecursiveFilterIterator {
public static $FILTERS = array(
'.htaccess',
'.html',
);
public function accept() {
return !in_array(
$this->current()->getFilename(),
self::$FILTERS,
true
);
}
}
$iterator = new RecursiveDirectoryIterator($dir);
$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
$filter = new MyRecursiveFilterIterator($iterator);
$all_files = new RecursiveIteratorIterator($filter,RecursiveIteratorIterator::SELF_FIRST);
to list all files and folders within specified $dir path.
MyRecursiveFilterIterator extends default RecursiveFilterIterator and excludes certain file extensions like .htaccess and html. Now I would like to add the ability to only show certain file types like .jpg, .png as well as keep excluded ones.
Upvotes: 0
Views: 231
Reputation:
Hi,
I only refer to to the first part of your question, which is: "...Now I would like to add the ability to only show certain file types like .jpg, .png ..."
So if you want to filter for certain file types you can use the RegexIterator, which is shown here in principle for .png files.
<?php
$Directory = new RecursiveDirectoryIterator("../");
$Iterator = new RecursiveIteratorIterator($Directory);
// here the .png Match is set
$Regex = new RegexIterator($Iterator, '/^.+\.png$/i', RecursiveRegexIterator::GET_MATCH);
// the following part is only for the output of the resulting array $Regex in html
$output = "<table width='100%' align='center'><table width='50%' align='center'>";
foreach($Regex as $key => $var) {
$output .= '<tr>';
foreach($var as $col => $val) {
$output .= "<td style='font-size:14px;font-weight:bold;'>" . $col . '</td>';
}
$output .= '</tr>';
foreach($var as $col => $val) {
$output .= '<td>' . $val . '</td>';
}
$output .= '</tr>';
}
$output .= '</table></table>';
echo $output;
?>
<head>
<style type="text/css">
table , tr , td {
font: arial;
font-size: 13px;
font-style: normal;
font-weight: normal;
border: 1px solid blue;
border-collapse: collapse;
}
</style>
</head>
... and so on. Naturally this is only a rough solution, which only shows the principle.
Best regards Axel Arnold Bangert - Herzogenrath 2016
Upvotes: 1