A class used to scan through files with a pattern specified by user. Read class comments for further details.
Code: Select all
<?php
/**
* class scanfiles (php5)
*
* Object used to scan through a directory matching filesnames
*
* Features :
* - case insentive (by default)
* - can pass array of patterns to match multiple filenames
* - checks whether directory exists
*
* Usage :
* - Array of supplied patterns
* $files = new ScanFiles(array('index.html', 'config.php'));
* print_r($files->search());
* - Array of supplied patterns with supplied directory
* $files = new ScanFiles(array('index.html', 'config.php'));
* print_r($files->search());
* - Array of supplied patterns with wildcard
* $files = new ScanFiles(array('index.*', 'ind*.php'));
* print_r($files->search());
* - String supplied pattern with case sensitivity
* $files = new ScanFiles('index.php', '', true);
* print_r($files->search());
*
* Feel free to modify and use this code at will
*
* Created by Jcart at http://devnetwork.net
* Special thanks to Feyd for helping with regex
*/
class scanFiles
{
protected $pattern, $dir, $extension;
/**
* Constructor
*
* Checks for invalid values and assigns default values
*
* @param {mixed} $pattern
* @param {string} $dir
* @param {bool} $sensitive
*/
public function __construct($pattern, $dir = '', $sensitive = false)
{
$this->directory = $this->getDir($dir);
$this->sensitive = $this->getSensitive($sensitive);
$this->pattern = $this->getPattern($pattern);
}
/**
* Search
*
* Perform search of directory
*
* @ returns {array}
*/
public function search()
{
$stack = array();
foreach (glob($this->directory .'*') as $file) {
$file = basename($file);
if (preg_match($this->pattern, $file) && !is_dir($this->directory.$file)) {
array_push($stack, $file);
}
}
return $stack;
}
/**
* getPattern
*
* Format pattern into regular expression
*
* @param {string} $pattern
* @returns {string}
*/
protected function getPattern($pattern)
{
if (is_array($pattern) && count($pattern)) {
$pattern = implode('|', array_map(create_function('$a', 'return preg_quote($a, \'#\');'), $pattern));
} elseif (!is_string($pattern)) {
throw new Exception('Pattern must be string or array');
}
$pattern = '#^(?:' . $pattern .')$#';
if (!$this->sensitive) {
$pattern .= 'i';
}
$pattern = str_replace('\*', '*', $pattern);
$pattern = str_replace('*', '.*?', $pattern);
return $pattern;
}
/**
* getSensitive
*
* Sets sensitivity of pattern matching
*
* @param {bool} $sensitive
* @returns {bool}
*/
protected function getSensitive($sensitive)
{
if (!is_bool($sensitive)) {
throw new Exception('Sensitivity must be boolean');
}
return $sensitive;
}
/**
* getDir
*
* If a directory was supplied we want to make sure it
* exists. Also checks whether to add an additional
* directory seperator
*
* @param {string} $dir
* @returns {string}
*/
protected function getDir($dir)
{
if (!empty($dir) && !file_exists($dir)) {
throw new Exception('Directory "'. $dir .'" cannot be found');
}
return (substr($dir, -1, 1) == DIRECTORY_SEPARATOR ? $dir : $dir . DIRECTORY_SEPARATOR);
}
}
?>Code: Select all
echo '<pre>';
echo '<h3>Listing all available files in directory</h3>';
echo '<pre>';
print_r(glob('/*'));
echo '<h3>Searching for array of matches - Expect Pass</h3>';
$files = new ScanFiles(array('config.sys', 'boot.bak'));
print_r($files->search());
echo '<h3>Searching for array of matches case sensitive - Expect Empty</h3>';
$files = new ScanFiles(array('config.sys', 'boot.bak'), '', true);
print_r($files->search());
echo '<h3>Searching for array of matches case sensitive - Expect Pass</h3>';
$files = new ScanFiles(array('CONFIG.SYS', 'BOOT.BAK'), '', true);
print_r($files->search());
echo '<h3>Searching for array of matches case sensitive with wildcard - Expect Pass</h3>';
$files = new ScanFiles(array('CONFIG*', 'B*.BAK'), '', true);
print_r($files->search());
echo '<h3>Searching for string matches with wildcard- Expect Pass</h3>';
$files = new ScanFiles('*fig*');
print_r($files->search());
#echo '<h3>Searching in non-existant directory - Expect Fail</h3>';
#$files = new ScanFiles('config', '/dir/does/not/exists/');
#print_r($files->search());Code: Select all
Array
(
[0] => /ATI
[1] => /AUTOEXEC.BAT
[2] => /Aplpications
[3] => /Applications
[4] => /BOOT.BAK
[5] => /CONFIG.SYS
)