getPathname()); } // Delete sub-directories foreach (File::directories($path) as $dir) { File::deleteDirectory($dir); } if (! $preserveRoot) { File::deleteDirectory($path); } } elseif (File::isFile($path)) { File::delete($path); } } /** * List files under a directory. * If $pattern is provided, return an array of preg_match($pattern, $relativePath, $matches) arrays, * where $matches[0] is the ABSOLUTE path for convenience (remaining capture groups preserved if present). * Otherwise return an array of SplFileInfo. * * @return array|\SplFileInfo> */ public function listFiles(string $path, string $pattern = ''): array { try { $files = File::allFiles($path); } catch (\Throwable $e) { throw new \RuntimeException('ERROR: Could not open temp dir: '.$e->getMessage()); } if ($pattern !== '') { $filtered = []; $base = rtrim($path, '/\\'); foreach ($files as $file) { $relative = $file->getRelativePathname(); if (preg_match($pattern, $relative, $matches)) { // Overwrite full match with absolute path to make downstream file ops straightforward $matches[0] = $base.'/'.$relative; $filtered[] = $matches; } } return $filtered; } return $files; } }