note 114184 added to function.scandir
| From: | gambit_642AThotmailDOTcom | Date: | Wed, 22 Jan 2014 02:04:08 +0000 |
| Subject: | note 114184 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198297@lists.php.net to get a copy of this message | ||
Needed something that could return the contents of single or multiple directories, recursively or
non-recursively,
for all files or specified file extensions that would be
accessible easily from any scope or script.
And I wanted to allow overloading cause sometimes I'm too lazy to pass all params.
<?php
class scanDir {
static private $directories, $files, $ext_filter, $recursive;
// ----------------------------------------------------------------------------------------------
// scan(dirpath::string|array, extensions::string|array, recursive::true|false)
static public function scan(){
// Initialize defaults
self::$recursive = false;
self::$directories = array();
self::$files = array();
self::$ext_filter = false;
// Check we have minimum parameters
if(!$args = func_get_args()){
die("Must provide a path string or array of path strings");
}
if(gettype($args[0]) != "string" && gettype($args[0]) != "array"){
die("Must provide a path string or array of path strings");
}
// Check if recursive scan | default action: no sub-directories
if(isset($args[2]) && $args[2] == true){self::$recursive = true;}
// Was a filter on file extensions included? | default action: return all file types
if(isset($args[1])){
if(gettype($args[1]) == "array"){self::$ext_filter = array_map('strtolower',
$args[1]);}
else
if(gettype($args[1]) == "string"){self::$ext_filter[] = strtolower($args[1]);}
}
// Grab path(s)
self::verifyPaths($args[0]);
return self::$files;
}
static private function verifyPaths($paths){
$path_errors = array();
if(gettype($paths) == "string"){$paths = array($paths);}
foreach($paths as $path){
if(is_dir($path)){
self::$directories[] = $path;
$dirContents = self::find_contents($path);
} else {
$path_errors[] = $path;
}
}
if($path_errors){echo "The following directories do not exists<br
/>";die(var_dump($path_errors));}
}
// This is how we scan directories
static private function find_contents($dir){
$result = array();
$root = scandir($dir);
foreach($root as $value){
if($value === '.' || $value === '..') {continue;}
if(is_file($dir.DIRECTORY_SEPARATOR.$value)){
if(!self::$ext_filter || in_array(strtolower(pathinfo($dir.DIRECTORY_SEPARATOR.$value,
PATHINFO_EXTENSION)), self::$ext_filter)){
self::$files[] = $result[] = $dir.DIRECTORY_SEPARATOR.$value;
}
continue;
}
if(self::$recursive){
foreach(self::find_contents($dir.DIRECTORY_SEPARATOR.$value) as $value) {
self::$files[] = $result[] = $value;
}
}
}
// Return required for recursive search
return $result;
}
}
?>
Usage:
scanDir::scan(path(s):string|array, [file_extensions:string|array], [subfolders?:true|false]);
<?php
//Scan a single directory for all files, no sub-directories
$files = scanDir::scan('D:\Websites\temp');
//Scan multiple directories for all files, no sub-dirs
$dirs = array(
'D:\folder';
'D:\folder2';
'C:\Other';
);
$files = scanDir::scan($dirs);
// Scan multiple directories for files with provided file extension,
// no sub-dirs
$files = scanDir::scan($dirs, "jpg");
//or with an array of extensions
$file_ext = array(
"jpg",
"bmp",
"png"
);
$files = scanDir::scan($dirs, $file_ext);
// Scan multiple directories for files with any extension,
// include files in recursive sub-folders
$files = scanDir::scan($dirs, false, true);
// Multiple dirs, with specified extensions, include sub-dir files
$files = scanDir::scan($dirs, $file_ext, true);
?>
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/function.scandir.php
Edit -- https://master.php.net/note/edit/114184
Del: integrated -- https://master.php.net/note/delete/114184/integrated
Del: useless -- https://master.php.net/note/delete/114184/useless
Del: bad code -- https://master.php.net/note/delete/114184/bad+code
Del: spam -- https://master.php.net/note/delete/114184/spam
Del: non-english -- https://master.php.net/note/delete/114184/non-english
Del: in docs -- https://master.php.net/note/delete/114184/in+docs
Del: other reasons-- https://master.php.net/note/delete/114184
Reject -- https://master.php.net/note/reject/114184
Search -- https://master.php.net/manage/user-notes.php