note 126262 added to class.filesystemiterator

From: Date: Thu, 22 Jul 2021 06:20:07 +0000
Subject: note 126262 added to class.filesystemiterator
Groups: php.notes 
Request: Send a blank email to php-notes+get-216633@lists.php.net to get a copy of this message
Here's a great little drop in replacement for FilesystemIterator I wrote to easily Iterate your filesystem, including: * Sorting - using ArrayIterator * Regex Matching - using RegexIterator * Limiting - using LimitIterator It's fully chainable <?php // Sort by filemtime $files = (new AdvancedFilesystemIterator('/path/to/files'))->sortByMTime(); // Sort by filemtime -> Limit output to 10 $files = (new AdvancedFilesystemIterator('/path/to/files'))->sortByMTime()->limit(0, 10); // Sort by filemtime -> Only get CSV files -> Limit to 10 $files = (new AdvancedFilesystemIterator('/path/to/files'))->sortByMTime()->match('/csv$/')->limit(0, 10); // Sort by filemtime -> Only get CSV files -> Limit to 10 -> and back to sorting by Filename $files = (new AdvancedFilesystemIterator('/path/to/files'))->sortByMTime()->match('/csv$/')->limit(0, 10)->sortByFilename(); // Sort by any of SplFileInfo's get*() methods i.e. Owner, CTime, Basename, ATime, Perms, Type, isFile, anything $files = (new AdvancedFilesystemIterator('/path/to/files'))->sortByOwner(); // Foreach foreach ((new AdvancedFilesystemIterator('/path/to/files'))->sortByMTime()->match('/csv$/')->limit(0, 10) AS $file) { print $file->getFilename() . "<br>\n"; } // The Class class AdvancedFilesystemIterator extends ArrayIterator { public function __construct(string $path, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS) { parent::__construct(iterator_to_array(new FilesystemIterator($path, $flags))); } public function __call(string $name, array $arguments) { if (preg_match('/^sortBy(.*)/', $name, $m)) return $this->sort('get' . $m[1]); throw new MemberAccessException('Method ' . $methodName . ' not exists'); } public function sort($method) { if (!method_exists('SplFileInfo', $method)) throw new InvalidArgumentException(sprintf('Method "%s" does not exist in SplFileInfo', $method)); $this->uasort(function(SplFileInfo $a, SplFileInfo $b) use ($method) { return (is_string($a->$method()) ? strnatcmp($a->$method(), $b->$method()) : $b->$method() - $a->$method()); }); return $this; } public function limit(int $offset = 0, int $limit = -1) { return parent::__construct(iterator_to_array(new LimitIterator($this, $offset, $limit))) ?? $this; } public function match(string $regex, int $mode = RegexIterator::MATCH, int $flags = 0, int $preg_flags = 0) { return parent::__construct(iterator_to_array(new RegexIterator($this, $regex, $mode, $flags, $preg_flags))) ?? $this; } } ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 103.133.52.236) ---- Manual Page -- https://php.net/manual/en/class.filesystemiterator.php Edit -- https://main.php.net/note/edit/126262 Del: integrated -- https://main.php.net/note/delete/126262/integrated Del: useless -- https://main.php.net/note/delete/126262/useless Del: bad code -- https://main.php.net/note/delete/126262/bad+code Del: spam -- https://main.php.net/note/delete/126262/spam Del: non-english -- https://main.php.net/note/delete/126262/non-english Del: in docs -- https://main.php.net/note/delete/126262/in+docs Del: other reasons-- https://main.php.net/note/delete/126262 Reject -- https://main.php.net/note/reject/126262 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#216633) next »