php遍历目录下文件,并读取内容

<?php
echo "<h2>遍历目录下文件,并读取内容</h2><br>
"; 
function listDir($dir)
{
	if(is_dir($dir))
   	{
     	if ($dh = opendir($dir)) 
		{
        	while (($file = readdir($dh)) !== false)
			{
     			if((is_dir($dir."/".$file)) && $file!="." && $file!="..")
				{
     				echo "<b>",$file,"/</b>";
     				listDir($dir."/".$file."/");
     			}
				else
				{
         			if($file!="." && $file!="..")
					{
         				echo '<h3>'.$file."</h3><br>";
						//逐行读取数据
						$file = fopen($dir.''.$file,"r");
						while(! feof($file))
						  {
						  echo '<xmp>'.fgets($file). "</xmp>";
						  }
						fclose($file);
      				}
     			}
				
        	}
        	closedir($dh);
     	}
   	}
}
//开始运行
//
echo "<b>文档标题</b><br/>";
listDir("D:/1/a/b/c/");
?>

  

原文地址:https://www.cnblogs.com/Tiago/p/5692264.html