最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】PHP中用dir的read或readdir,读取目录下的文件,输出的文件名,始终为1

PHP crifan 3240浏览 0评论

【问题】

当前环境:

WordPress 3.4 with DB 20596 on PHP 5.3.6

Plugin version: 3.2.7 ($Id: sitemap-core.php 440117 2011-09-19 13:24:49Z arnee $)

问题:

在wordpress的google xml sitemap插件中,

参考:

用PHP遍历当前目录下所有文件

已经实现了相应的代码:

                //http://localhost/files/doc/docbook/
                //files/doc/docbook/
                //files/doc/docbook
                //http://localhost/wp-admin/options-general.php?page=google-sitemap-generator/sitemap.php&sm_wpv=3.4&sm_pv=3.2.7
                echo "------------open dir begin --------------------<br />";
                $curPwd = getcwd();
                echo "curPwd=".$curPwd."<br />";
                echo $_SERVER['DOCUMENT_ROOT']."<br>"; //获得服务器文档根变量
                $rootPath = $_SERVER['DOCUMENT_ROOT'];
                $rootPath = $rootPath."/";
                //$curPath = $p->GetUrl();
                //$curPath = "../files/doc/docbook";
                $curPath = "../files/doc/docbook/";
                //$curPath = "files/doc/docbook";
                echo "curPath=" . $curPath."<br />";
                
                $isDir = is_dir($curPath);
                echo "isDir=".$isDir."<br />";
                
                //$fullPath = $rootPath.$curPath;
                $fullPath = $curPath;
                echo "fullPath=" . $fullPath."<br />";
                echo "---test dir()---</br />";
                $openedDir = dir($fullPath);
                while($file = $openedDir->read() !== false)
                {
                    echo "file=". $file."<br />";
                }
                $openedDir->close();
                
                echo "---test opendir()---</br />";
                $dirHandler = opendir($fullPath);
                //$dirHandler = opendir(".");
                while($filename = readdir($dirHandler) !== false)
                {
                    echo "filename=". $filename."<br />";
                    //echo "filename: $filename : filetype: " . filetype($fullPath . $filename) . "\n";
                }
                closedir($dirHandler);
                echo "------------open dir end --------------------<br />";
                //echo phpinfo();
                echo "---------------------------------------------<br />";

用于读取对应目录下的所有的文件,输出为:

------------open dir begin --------------------
curPwd=D:\tmp\WordPress\DevRoot\httpd-2.2.19-win64\httpd-2.2-x64\htdocs\wp-admin
D:/tmp/WordPress/DevRoot/httpd-2.2.19-win64/httpd-2.2-x64/htdocs
curPath=../files/doc/docbook/
isDir=1
fullPath=../files/doc/docbook/
---test dir()---
file=1
file=1
file=1
file=1
file=1
file=1
---test opendir()---
filename=1
filename=1
filename=1
filename=1
filename=1
filename=1
------------open dir end --------------------
---------------------------------------------

其中,对应的目录files/doc/docbook下有三个目录和一个文件:

files under dir

但是上面的输出中,文件名filename却始终为1.

(其中第一和第二个filename对应的是当前路径的“.”和上一级目录的“..”)

【解决过程】

1.以为是wordpress其他插件可能会影响php执行,所以去把wordpress中,除了google xml sitemap之外的插件,基本上都停用了,问题依旧。

2.参考:

dir

opendir

PHP dir() 函数

PHP Directory 函数

等内容,才写出前面的那些代码,结果证明,不论是dir的read还是readdir,问题都是一样的。

3.以为是绝对路径有问题,所以改为上面的相对路径的版本,测试结果问题依旧。

4.网上找了半天,也找不到,关于php的readdir的输出文件名始终为1的类似问题。

5.后来经过折腾,添加了手动的readdir,相应代码变为:

                $isDir = is_dir($curPath);
                echo "isDir=".$isDir."<br />";
                
                //$fullPath = $rootPath.$curPath;
                $fullPath = $curPath;
                echo "fullPath=" . $fullPath."<br />";
                echo "---test dir()---</br />";
                $openedDir = @dir($fullPath);
                
                echo "first  read output=".$openedDir->read()."<br />";
                echo "second read output=".$openedDir->read()."<br />";
                echo "third  read output=".$openedDir->read()."<br />";
                
                while($file = $openedDir->read() !== false)
                {
                    echo "file=". $file."<br />";
                }
                $openedDir->close();
                
                echo "---test opendir()---</br />";
                $dirHandler = @opendir($fullPath);
                //$dirHandler = opendir(".");
                
                echo "first  read output=".readdir($dirHandler)."<br />";
                echo "second read output=".readdir($dirHandler)."<br />";
                echo "third  read output=".readdir($dirHandler)."<br />";
                
                while($filename = readdir($dirHandler) !== false)
                {
                    echo "filename=". $filename."<br />";
                    //echo "filename: $filename : filetype: " . filetype($fullPath . $filename) . "\n";
                }
                closedir($dirHandler);

结果输出得到正常的结果:

------------open dir begin --------------------
curPwd=D:\tmp\WordPress\DevRoot\httpd-2.2.19-win64\httpd-2.2-x64\htdocs\wp-admin
D:/tmp/WordPress/DevRoot/httpd-2.2.19-win64/httpd-2.2-x64/htdocs
curPath=../files/doc/docbook/
isDir=1
fullPath=../files/doc/docbook/
---test dir()---
first read output=.
second read output=..
third read output=html
file=1
file=1
file=1
---test opendir()---
first read output=.
second read output=..
third read output=html
filename=1
filename=1
filename=1
------------open dir end --------------------
---------------------------------------------

可见,看来是

while($file = $openedDir->read() !== false)

的问题,然后此时才注意到,两个操作符:

等于号“=”

不等于“!==”

两者的运算优先级是,等于号小于不等于“!==”

所以,应该加上括号,写成:

while(($file = $openedDir->read()) !== false)
...
while(($filename = readdir($dirHandler)) !== false)

这样才能保证file和filename得到正确的赋值。

【总结】

不论什么语言,写代码的时候,关于多个运算符在一起,都还是要养成加括号的好习惯,才可以。

转载请注明:在路上 » 【已解决】PHP中用dir的read或readdir,读取目录下的文件,输出的文件名,始终为1

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.174 seconds, using 22.14MB memory