Home > Article > Backend Development > Why does the PHP readdir function have different results every time it is executed?
<code><?php $path = dirname(__FILE__); $dir = opendir($path); while($file = readdir($dir)) { echo "$file\n"; }</code>
The code is as above:
There are two execution results.
The first kind
The second kind
Under win7 environment, can this function of php 5.6 be executed normally in linux environment?
<code><?php $path = dirname(__FILE__); $dir = opendir($path); while($file = readdir($dir)) { echo "$file\n"; }</code>
The code is as above:
There are two execution results.
The first kind
The second kind
Under win7 environment, can this function of php 5.6 be executed normally in linux environment?
Refer to php official documentation
<code>$path = dirname(__FILE__); $dir = opendir($path); while(false!==($file = readdir($dir))) { echo "$file\n"; }</code>
This is the correct way you should write it, with official documentation attached
<code>使用readdir函数成功则返回文件名 或者在失败时返回 FALSE </code>
<code>此函数可能返回布尔值 FALSE,但也可能返回等同于 FALSE 的非布尔值。 请阅读 布尔类型章节以获取更多信息。应使用 === 运算符来测试此函数的返回值。 </code>
See if you have a file or directory named 0?
It’s a permission issue... The process user should be PHP or Apache, not your login account.