Bug #80056 [Opn->Nab]: missing files when using `foreach` to iterate `RecursiveDirectoryIterator`

From: Date: Tue, 15 Sep 2020 15:26:39 +0000
Subject: Bug #80056 [Opn->Nab]: missing files when using `foreach` to iterate `RecursiveDirectoryIterator`
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-229015@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80056&edit=1 ID: 80056 Updated by: cmb@php.net Reported by: 397836525 at qq dot com Summary: missing files when using foreach to iterate RecursiveDirectoryIterator -Status: Open +Status: Not a bug Type: Bug Package: *Directory/Filesystem functions Operating System: Windows PHP Version: 7.4.10 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: Thanks! Then this is unlikely to be a PHP issue. Previous Comments: ------------------------------------------------------------------------ [2020-09-07 07:20:03] 397836525 at qq dot com Hi there, I finally find out that the reason to problem is actually the environment. I was using the docker with the WSL 2 based engine under the Windows system. As soon as I changed the docker engine into Legacy Hyper-V backend, the problem disappeared. ------------------------------------------------------------------------ [2020-09-07 07:15:59] cmb@php.net For some reason I missed that script. Anyhow, I cannot reproduce the issue with that script. All six methods return the expected 65. ------------------------------------------------------------------------ [2020-09-04 08:23:25] 397836525 at qq dot com Somehow I cannot upload my test code by using patch file.So I paste the test code in the previos comment.please check. ------------------------------------------------------------------------ [2020-09-04 08:15:00] cmb@php.net I cannot reproduce the reported behavior. > please use the patch file to reproduce the bug. Not sure if you tried to add some file as patch, but that won't necessarily work. Please make such scripts publicly available somewhere else (e.g. gist.github.com, pastebin.com), and share the link here. ------------------------------------------------------------------------ [2020-09-04 08:08:44] 397836525 at qq dot com here's the code for reproducing the bug. <?php /** * Created by RexMin. * Description. * Email: rexmin@futunn.com * Date: 2020/9/4 * Time: 14:40 */ class Test { protected $path = 'directory'; public function createTestFiles () { $amount = 63; if (!is_dir($this->path)) { mkdir($this->path); } foreach (scandir($this->path) as $filename) { @unlink("{$this->path}/$filename"); } for ($num = 1; $num <= $amount; $num++) { fopen("{$this->path}/{$num}.txt", "a"); } } public function runTest() { echo 'get by foreach: ' . $this->getByForeach() . PHP_EOL; echo 'get by foreach with rewind: ' . $this->getByForeachWithRewind() . PHP_EOL; echo 'get by foreach after foreach: ' . $this->getByForeachAfterForeach() . PHP_EOL; echo 'get by loop: ' . $this->getByloop() . PHP_EOL; echo 'get by iterator_to_array: ' . $this->getByIteratorToArray() . PHP_EOL; echo 'get by iterator_to_array with rewind: ' . $this->getByIteratorToArrayWithRewind() . PHP_EOL; } public function getByForeach() { $directory = new RecursiveDirectoryIterator($this->path); $count = 0; foreach ($directory as $filename => $fileInfo) { $count++; } return $count; } public function getByForeachWithRewind() { $directory = new RecursiveDirectoryIterator($this->path); $count = 0; $directory->rewind(); foreach ($directory as $filename => $fileInfo) { $count++; } return $count; } public function getByForeachAfterForeach() { $directory = new RecursiveDirectoryIterator($this->path); $count = 0; foreach ($directory as $filename => $fileInfo) { // do nothing } foreach ($directory as $filename => $fileInfo) { $count++; } return $count; } public function getByLoop() { $directory = new RecursiveDirectoryIterator($this->path); $count = 0; while ($directory->valid()) { $count++; $directory->next(); } return $count; } public function getByIteratorToArray() { $directory = new RecursiveDirectoryIterator($this->path); return count(iterator_to_array($directory)); } public function getByIteratorToArrayWithRewind() { $directory = new RecursiveDirectoryIterator($this->path); $directory->rewind(); return count(iterator_to_array($directory)); } } $test = new Test(); $test->createTestFiles(); $test->runTest(); ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=80056 -- Edit this bug report at https://bugs.php.net/bug.php?id=80056&edit=1

« previous php.bugs (#229015) next »