note 92710 added to function.glob
| From: | uramihsayibok at osu1 dot php dot net | Date: | Tue, 04 Aug 2009 22:38:35 +0000 |
| Subject: | note 92710 added to function.glob | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159117@lists.php.net to get a copy of this message | ||
Those of you with PHP 5 don't have to come up with these wild functions to scan a directory
recursively: the SPL can do it.
<?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
----
Server IP: 64.71.164.2
Probable Submitter: 64.81.169.38
----
Manual Page -- http://www.php.net/manual/en/function.glob.php
Edit -- https://master.php.net/note/edit/92710
Del: integrated -- https://master.php.net/note/delete/92710/integrated
Del: useless -- https://master.php.net/note/delete/92710/useless
Del: bad code -- https://master.php.net/note/delete/92710/bad+code
Del: spam -- https://master.php.net/note/delete/92710/spam
Del: non-english -- https://master.php.net/note/delete/92710/non-english
Del: in docs -- https://master.php.net/note/delete/92710/in+docs
Del: other reasons-- https://master.php.net/note/delete/92710
Reject -- https://master.php.net/note/reject/92710
Search -- https://master.php.net/manage/user-notes.php