note 111670 added to ref.filesystem
| From: | ob at babcom dot biz | Date: | Thu, 14 Mar 2013 15:56:22 +0000 |
| Subject: | note 111670 added to ref.filesystem | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194072@lists.php.net to get a copy of this message | ||
a function based on "tunnelareaten at gmail dot com"s idea to search for files in a given
directory by a searchstring or by fileextension.
I added support to search recursively through all sub-directories an to determine weather the
filepath should be returned or not.
<?php
// recursive function to get contents of given folder by searchterm or fileextension
// (does not show folders)
// standards: Foldername: string
// Searchterm: string
// Searchtype: ext/search (file-extension or searchterm within filename)
// SaveCompletePath: true/1
// usage: array FileSearch_r($Folder,$Search[,$SearchType,$SavePath])
function FileSearch_r($Dir,$Search,$SearchType="search",$SavePath=1) {
$Array=array();
$D=dir($Dir);
while (false!==($Entry=$D->read()))
if ($Entry!='.' && $Entry!='..') {
$Entry=$Dir.$Entry;
if (is_dir($Entry))
$Array=array_merge($Array,FileSearch_r($Entry.'/',$Search,$SearchType,$SavePath));
else
if ($SearchType=="search"
?substr_count($Entry,$Search)>0
:($SearchType=="ext"
?substr($Entry,-strlen($Search))===$Search
:true))
$Array[]=$Entry;
}
$D->close();
sort($Array,SORT_STRING);
if(!(bool)$SavePath) $Array=str_replace($Dir,"",array_values($Array));
return $Array;
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 94.101.43.169
----
Manual Page -- http://php.net/manual/en/ref.filesystem.php
Edit -- https://master.php.net/note/edit/111670
Del: integrated -- https://master.php.net/note/delete/111670/integrated
Del: useless -- https://master.php.net/note/delete/111670/useless
Del: bad code -- https://master.php.net/note/delete/111670/bad+code
Del: spam -- https://master.php.net/note/delete/111670/spam
Del: non-english -- https://master.php.net/note/delete/111670/non-english
Del: in docs -- https://master.php.net/note/delete/111670/in+docs
Del: other reasons-- https://master.php.net/note/delete/111670
Reject -- https://master.php.net/note/reject/111670
Search -- https://master.php.net/manage/user-notes.php