PHP遍历目录下的文件夹和文件 以及遍历文件下内容

时间:2023-03-08 18:56:27
PHP遍历目录下的文件夹和文件 以及遍历文件下内容

1.遍历目录下的文件夹和文件:

 public function bianli1($dir)
{
$files = array();
if($head = opendir($dir))
{
while(($file = readdir($head)) !== false)
{
if($file != ".." && $file!=".")
{
if(is_dir($file))
{
$files[$file]=bianli1($dir.'/'.$file);
}
else
{
$files[]=$file;
}
}
}
}
closedir($head);
return $files;
}

2.遍历文件下内容:

 function bianli2()
{
$hostdir= iconv("utf-8","gbk","D:/wamp/www") ;
$filesnames = scandir($hostdir);
foreach ($filesnames as $name) {
if($name!=".." && $name!=".")
{
$cipath = $hostdir.$name;
$cjfilenames = scandir($cipath);
foreach($cjfilenames as $cjname)
{
if($cjname!=".." && $cjname!="." &&!is_dir($cipath."/".$cjname))
{
$str = file_get_contents($cipath."/".$cjname);
file_put_contents("D:/test.txt",$str,FILE_APPEND);
}
}
}
echo "\n";
}
}