note 119422 added to function.scandir
| From: | simon dot riget at gmail dot com | Date: | Thu, 02 Jun 2016 19:28:33 +0000 |
| Subject: | note 119422 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205643@lists.php.net to get a copy of this message | ||
This is a simple and versatile function that returns an array tree of files, matching wildcards:
<?php
// List files in tree, matching wildcards * and ?
function tree($path){
static $match;
// Find the real directory part of the path, and set the match parameter
$last=strrpos($path,"/");
if(!is_dir($path)){
$match=substr($path,$last);
while(!is_dir($path=substr($path,0,$last)) && $last!==false)
$last=strrpos($path,"/",-1);
}
if(empty($match)) $match="/*";
if(!$path=realpath($path)) return;
// List files
foreach(glob($path.$match) as $file){
$list[]=substr($file,strrpos($file,"/")+1);
}
// Process sub directories
foreach(glob("$path/*", GLOB_ONLYDIR) as $dir){
$list[substr($dir,strrpos($dir,"/",-1)+1)]=tree($dir);
}
return @$list;
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 109.198.131.34
----
Manual Page -- http://php.net/manual/en/function.scandir.php
Edit -- https://master.php.net/note/edit/119422
Del: integrated -- https://master.php.net/note/delete/119422/integrated
Del: useless -- https://master.php.net/note/delete/119422/useless
Del: bad code -- https://master.php.net/note/delete/119422/bad+code
Del: spam -- https://master.php.net/note/delete/119422/spam
Del: non-english -- https://master.php.net/note/delete/119422/non-english
Del: in docs -- https://master.php.net/note/delete/119422/in+docs
Del: other reasons-- https://master.php.net/note/delete/119422
Reject -- https://master.php.net/note/reject/119422
Search -- https://master.php.net/manage/user-notes.php