Re: fopen('.', 'r') succeedes !?
| From: | Christoph Becker | Date: | Mon, 02 Mar 2015 12:52:23 +0000 |
| Subject: | Re: fopen('.', 'r') succeedes !? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-324567@lists.php.net to get a copy of this message | ||
Umberto Salsi wrote:
> Discovered today: fopen() opens '.' and '..' but fread() returns nothing
> without errors nor end-of-file, so this loop:
>
> $f = fopen('.', 'r');
> if( $f === FALSE ) die("fopen failed");
> $n = 0;
> while(($s = fread($f, 10)) !== FALSE)
> $n += strlen($s);
> fclose($f);
> echo "total bytes read: $n\n";
>
> never ends! The same happens with '..'. Tested on PHP 5.6.3 and 7.0.0
> under Slackware 14.1 32-bits. Under Vista, instead, fopen() fails as
> expected.
>
> Bug or specific feature under Linux?
According to the manual[]1] that is a feature:
| This function *may* also succeed when filename is a directory. If you
| are unsure whether filename is a file or a directory, you may need to
| use the is_dir() function before calling fopen().
(Emphasis mine.)
[1]
<http://php.net/manual/en/function.fopen.php#refsect1-function.fopen-notes>
[2] <http://php.net/manual/en/function.fopen.php#55434>
[3] <http://php.net/manual/en/function.fopen.php#37761>
There are some user contributed notes[2][3] on the manual page which
clarify what's happening.
I'm not sure why there's an infinite loop -- have you tried feof()
instead of relying on the return value of fread().
--
Christoph M. Becker