is_dir() 在 php 中扫描目录时失败
我正在尝试为主文件夹中的每个目录构建一个带有
的
列表。 这是我的代码:<?php
$dir = opendir(getEnv("DOCUMENT_ROOT") . "/folder");
while(($file = readdir($dir)) !== false ){
if( is_dir($file) && $file !== "." && $file !== ".." ){
echo "<li>" . $file . "</li>";
}
}
closedir($dir);
?>
/home/example/folder中有两个目录,但不被识别为文件夹(当然它们是!)
如果我“echo
”在while循环中打印文件(它们很好地存在于脚本中,没有问题)。 如果我尝试“filetype
”它们,则会抛出 lstat failed
错误,我在互联网上搜索了它的含义,但最终除了我痛苦的技术支持之外什么也没有。去理解。
I'm trying to build a <ul>
list with a <li>
for each directory in a main folder.
here's my code :
<?php
$dir = opendir(getEnv("DOCUMENT_ROOT") . "/folder");
while(($file = readdir($dir)) !== false ){
if( is_dir($file) && $file !== "." && $file !== ".." ){
echo "<li>" . $file . "</li>";
}
}
closedir($dir);
?>
there are two directories in /home/example/folder but there are not recognized as folders (for sure they are !)
if I "echo
" the files in the while loop they are printed (they well exist for the script no trouble on that side).
If I try to "filetype
" them, a lstat failed
error is thrown, I searched on internet the meaning of it and I end up with nothing but technically support that I pain to understand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题是,在“文件夹”目录中,你可以有两个目录(a和b),然后读取检索“a”和“b”作为文件名,而is_dir接收完整路径文件名或相对文件名,你有两个选项:
Your problem is that inside "folder" directory you can have two directories (a and b),then the reading retrieve "a" and "b" as file names, while is_dir receive a full path filename or a relative filename, you have two options:
尝试:
不需要
!== false
部分。如果 readdir 到达末尾,则返回 false,循环将自动终止。您的版本被解析为:例如,您正在分配
readdir($file) !== false
的布尔结果,而不是 readdir 的返回值。扩展我下面的评论,关于 运算符优先级:
PHP 的解析树你的循环设置看起来像这样,扩展:
要解决这个问题,要么完全删除 !== false 部分,要么用额外的括号强制你自己的优先级:
Try:
The
!== false
portion is not required. if readdir reaches the end, it returns a false, and the loop will automatically terminate. Your version is being parsed as:e.g. you're assigning the boolean result of
readdir($file) !== false
, not the return value from readdir.to expand on my comment below, regarding operator precedence:
PHP's parse tree of your loop setup looks like this, expanded:
To fix this, either remove the !== false portion entirely, or enforce your own precedence with extra brackets: