1、遍历目录下的文件夹和文件
[PHP] 纯文本查看 复制代码 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、遍历文件下的内容:
[PHP] 纯文本查看 复制代码 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";
}
}
|