note 128311 added to function.opendir
| From: | AlexDawn at main dot php dot net | Date: | Wed, 15 Mar 2023 17:56:56 +0000 |
| Subject: | note 128311 added to function.opendir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220268@lists.php.net to get a copy of this message | ||
<?php
/**
* These functions pair up nicely with generators, to hide away
* all the messy file handlers (a bit like python's with blocks)
* remove the echos they are just to demo how the generator
* works with the foreach loop.
*
* @param string $filepath
* @return Generator<string>
*/
function generateFiles(string $filepath): Generator
{
echo "opening handle" . PHP_EOL;
$handle = opendir($filepath);
// looks more complex than needed but the docs says the type check is important
// https://www.php.net/manual/en/function.readdir.php
try {
while (false !== ($entry = readdir($handle))) {
yield $entry;
}
} finally {
closedir($handle);
echo "closed handle" . PHP_EOL;
}
}
foreach (generateFiles('.') as $file) {
echo $file . PHP_EOL;
}
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.130 (proxied: 78.144.128.129)
----
Manual Page -- https://php.net/manual/en/function.opendir.php
Edit -- https://main.php.net/note/edit/128311
Del: integrated -- https://main.php.net/note/delete/128311/integrated
Del: useless -- https://main.php.net/note/delete/128311/useless
Del: bad code -- https://main.php.net/note/delete/128311/bad+code
Del: spam -- https://main.php.net/note/delete/128311/spam
Del: non-english -- https://main.php.net/note/delete/128311/non-english
Del: in docs -- https://main.php.net/note/delete/128311/in+docs
Del: other reasons-- https://main.php.net/note/delete/128311
Reject -- https://main.php.net/note/reject/128311
Search -- https://main.php.net/manage/user-notes.php