note 120693 deleted from function.scandir by cmb
| From: | cmb@php.net | Date: | Wed, 22 Feb 2017 19:23:03 +0000 |
| Subject: | note 120693 deleted from function.scandir by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208808@lists.php.net to get a copy of this message | ||
Note Submitter: Kevin
----
BE AWARE! This omission cost me about 2 hours in debugging:
It's worth noting that the PHP 4 example given does not include the closedir($dh) peice of it.
I ran into a really frustrating bug where because I took it as "there is no need to close the
directory," the rmdir() command (used after this custom scandir example) could not delete the
empty directories I had created using PHP. In fact, when I tried to use the Linux command "ls
-l" it told me "Permission Denied!" (because it was opened, but not closed).
It should look like the example found on the http://au2.php.net/manual/en/function.opendir.php
opendir() function reference page:
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
With the closedir($dh) or resource that was passed to it.