Advertisement
am_dot_com

ACA 2020-12-22

Dec 22nd, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * receives a file system path
  5.  * receives a file extension
  6.  * receives a boolean that controls its (not) recursive behavior
  7.  *
  8.  * returns a collection of file system objects that match the request
  9.  */
  10. function fsItems(
  11.     string $pStrPath,
  12.     string $pStrExtension = "*", //accept all files, regardless of extension
  13.     bool $pbRecursive = true
  14. ){
  15.     $allFilesFound = [];
  16.     try {
  17.         $o = new DirectoryIterator ($pStrPath);
  18.         foreach($o as $fsItem){
  19.             $bIsDir = $fsItem->isDir();
  20.             //$bIsFile = !$bIsDir && !$fsItem->isLink();
  21.             $bIsFile = $fsItem->isFile();
  22.  
  23.             if ($bIsFile){
  24.                 $strExtension = $fsItem->getExtension();
  25.                 $bAcceptAll = $pStrExtension === "*";
  26.                 //$bExtensionMatch = $strExtension === $pStrExtension;
  27.                 $bExtensionMatch = strcasecmp($pStrExtension, $strExtension) === 0;
  28.  
  29.                 //example of reading specific dates
  30.                 $ctime = $fsItem->getCTime(); //when was it created?
  31.                 $atime = $fsItem->getATime(); //when was last access time?
  32.                 $mtime = $fsItem->getMTime(); //when was last modified time?
  33.  
  34.                 if ($bAcceptAll || $bExtensionMatch){
  35.                     $allFilesFound[] = clone($fsItem);
  36.                 }//if
  37.             }//found a file!
  38.  
  39.             if ($pbRecursive){
  40.                 $bIsDot = $fsItem->isDot();
  41.                 if ($bIsDir && !$bIsDot){
  42.                     $strStartingPath = $fsItem->getRealPath();
  43.                     $subFiles =
  44.                         fsItems(
  45.                             $strStartingPath,
  46.                             $pStrExtension,
  47.                             $pbRecursive
  48.                         );
  49.  
  50.                     $allFilesFound = array_merge(
  51.                         $subFiles,
  52.                         $allFilesFound
  53.                     );
  54.                 }//if
  55.             }//if recursive
  56.         }//for every item found
  57.     }
  58.     catch (Exception $e){
  59.         echo $e->getMessage();
  60.     }
  61.     return $allFilesFound;
  62. }//fsItems
  63.  
  64. define ("MY_PATH", "f:\\coding.jb\\php\\esgts\\aca2021");
  65.  
  66. $aca2021 = fsItems(
  67.     MY_PATH
  68. );
  69.  
  70.  
  71. $iHowManyFilesFound = count($aca2021);
  72.  
  73. var_dump($aca2021); //these are objects
  74. echo $iHowManyFilesFound . " file(s) were found!".PHP_EOL;
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement