Last active
March 7, 2016 14:36
-
-
Save BugBuster1701/4336433 to your computer and use it in GitHub Desktop.
Iteratoren in php u.a. als glob Ersatz per SPL um rekursiv zu suchen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $dir_iterator = new RecursiveDirectoryIterator("/path"); | |
| $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST); | |
| // could use CHILD_FIRST if you so wish | |
| foreach ($iterator as $file) { | |
| echo $file, "\n"; | |
| } | |
| ?> | |
| Not to mention the fact that $file will be an SplFileInfo class, so you can do powerful stuff really easily: | |
| <?php | |
| $size = 0; | |
| foreach ($iterator as $file) { | |
| if ($file->isFile()) { | |
| echo substr($file->getPathname(), 27) . ": " . $file->getSize() . " B; modified " . date("Y-m-d", $file->getMTime()) . "\n"; | |
| $size += $file->getSize(); | |
| } | |
| } | |
| echo "\nTotal file size: ", $size, " bytes\n"; | |
| ?> | |
| \Luna\luna.msstyles: 4190352 B; modified 2008-04-13 | |
| \Luna\Shell\Homestead\shellstyle.dll: 362496 B; modified 2006-02-28 | |
| \Luna\Shell\Metallic\shellstyle.dll: 362496 B; modified 2006-02-28 | |
| \Luna\Shell\NormalColor\shellstyle.dll: 361472 B; modified 2006-02-28 | |
| \Luna.theme: 1222 B; modified 2006-02-28 | |
| \Windows Classic.theme: 3025 B; modified 2006-02-28 | |
| Total file size: 5281063 bytes | |
| in Contao: | |
| <?php | |
| //https://github.com/contao/core/blob/ea9a00c7b34edea8b9bf41414bf66c620eb6a3fa/system/bin/ide_compat#L40-L57 | |
| // Recursively scan all subfolders | |
| $objFiles = new \RecursiveIteratorIterator( | |
| new \RecursiveDirectoryIterator( | |
| TL_ROOT . '/system/modules/' . $strModule, | |
| \FilesystemIterator::UNIX_PATHS|\FilesystemIterator::FOLLOW_SYMLINKS|\FilesystemIterator::SKIP_DOTS | |
| ) | |
| ); | |
| // Get all PHP files | |
| foreach ($objFiles as $objFile) { | |
| if (pathinfo($objFile->getFilename(), PATHINFO_EXTENSION) == 'php') { | |
| $strRelpath = str_replace(TL_ROOT . '/system/modules/' . $strModule . '/', '', $objFile->getPathname()); | |
| if (strncmp($strRelpath, 'assets/', 7) !== 0 && strncmp($strRelpath, 'config/', 7) !== 0 && strncmp($strRelpath, 'dca/', 4) !== 0 && strncmp($strRelpath, 'languages/', 10) !== 0 && strncmp($strRelpath, 'templates/', 10) !== 0) { | |
| $arrFiles[] = $strRelpath; | |
| } | |
| } | |
| } | |
| ?> | |
| https://schlueters.de/blog/archives/112-MySQLi-Resultset-Iterator.html | |
| http://www.a-coding-project.de/iteratoren-flexibele-arrays/ | |
| #fast nicht ganz | |
| http://www.a-coding-project.de/csv-import-in-php/ | |
| #Offtopic: Traits | |
| http://www.a-coding-project.de/traits-mehrfachvererbung-in-php/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment