Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * receives a file system path
- * receives a file extension
- * receives a boolean that controls its (not) recursive behavior
- *
- * returns a collection of file system objects that match the request
- */
- function fsItems(
- string $pStrPath,
- string $pStrExtension = "*", //accept all files, regardless of extension
- bool $pbRecursive = true
- ){
- $allFilesFound = [];
- try {
- $o = new DirectoryIterator ($pStrPath);
- foreach($o as $fsItem){
- $bIsDir = $fsItem->isDir();
- //$bIsFile = !$bIsDir && !$fsItem->isLink();
- $bIsFile = $fsItem->isFile();
- if ($bIsFile){
- $strExtension = $fsItem->getExtension();
- $bAcceptAll = $pStrExtension === "*";
- //$bExtensionMatch = $strExtension === $pStrExtension;
- $bExtensionMatch = strcasecmp($pStrExtension, $strExtension) === 0;
- //example of reading specific dates
- $ctime = $fsItem->getCTime(); //when was it created?
- $atime = $fsItem->getATime(); //when was last access time?
- $mtime = $fsItem->getMTime(); //when was last modified time?
- if ($bAcceptAll || $bExtensionMatch){
- $allFilesFound[] = clone($fsItem);
- }//if
- }//found a file!
- if ($pbRecursive){
- $bIsDot = $fsItem->isDot();
- if ($bIsDir && !$bIsDot){
- $strStartingPath = $fsItem->getRealPath();
- $subFiles =
- fsItems(
- $strStartingPath,
- $pStrExtension,
- $pbRecursive
- );
- $allFilesFound = array_merge(
- $subFiles,
- $allFilesFound
- );
- }//if
- }//if recursive
- }//for every item found
- }
- catch (Exception $e){
- echo $e->getMessage();
- }
- return $allFilesFound;
- }//fsItems
- define ("MY_PATH", "f:\\coding.jb\\php\\esgts\\aca2021");
- $aca2021 = fsItems(
- MY_PATH
- );
- $iHowManyFilesFound = count($aca2021);
- var_dump($aca2021); //these are objects
- echo $iHowManyFilesFound . " file(s) were found!".PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement