Re: fopen('.', 'r') succeeds !?
| From: | Tim Streater | Date: | Mon, 02 Mar 2015 21:05:00 +0000 |
| Subject: | Re: fopen('.', 'r') succeeds !? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-324573@lists.php.net to get a copy of this message | ||
On 02 Mar 2015 at 19:27, Umberto Salsi <salsi@icosaedro.it> wrote:
> salsi@icosaedro.it (Umberto Salsi) wrote:
>
>> Discovered today: fopen() opens '.'
>> [rest of my bogus comments removed]
>
> Filed https://bugs.php.net/bug.php?id=69163 about fopen()
> opening a directory
> under Linux, when a more reasonable behavior would be to raise E_WARNING.
> Testing for is_dir() is a weak workaroud nobody does and it is not atomic.
Disagree.
When scanning a directory I do this (leaving out error handling and recursion details):
$start = '/path/to/here'
$dirh = opendir ($start);
while (true)
{
$nextfile = readdir ($dirh);
if ($nextfile===false) break;
if ($nextfile=='.' || $nextfile=='..' || $nextfile[0]=='.')
continue;
if (is_dir($start . $nextfile)==true) // We found a folder
{
// Recurse into the folder
continue;
}
// Handle the file here
}
Seems to work.
--
Cheers -- Tim