首頁 >php教程 >PHP源码 >php遍历目录

php遍历目录

PHP中文网
PHP中文网原創
2016-06-01 14:32:441093瀏覽

1. [PHP]代码   

<?php
	
	//遍历文件夹
	echo memory_get_usage() . "\n";

	function traversalDir($path = &#39;.&#39;)
	{
		echo memory_get_usage() . "\n";
		$dir = opendir($path);
		echo memory_get_usage() . "\n";
		while (($file = readdir($dir)) !== false)
		{
			$sub_dir = $path . DIRECTORY_SEPARATOR . $file;    //构建子目录路径  directory_separator

			if ($file == "." || $file == "..")
			{
				continue;
			}
			else if (is_dir($sub_dir))
			{
				echo &#39;Directory &#39; . $file . &#39;:&#39; . "\n";
                traversalDir($sub_dir);
			}
			else 
			{
				echo &#39;File in Directory &#39; . $path . &#39;: &#39; . $file . "\n";
			}
		}
	}

	traversalDir();
	echo memory_get_usage() . "\n";

	echo memory_get_usage() . "\n";
	//使用PHP函数处理读取文件及文件夹.
	function loadReadDir($path = ".")
	{
		echo memory_get_usage() . "\n";
		$pathDir = dir($path);
		echo memory_get_usage() . "\n";
		while(($file = $pathDir->read()) !== false)
		{
			$dir = $path . DIRECTORY_SEPARATOR . $file;

			if ($file == "." || $file == "..")
			{
				continue;
			}
			else if (is_dir($dir))
			{
				echo "directory:" . $file . "\n";
				loadReadDir($dir);
			}
			else
			{
				echo "fileName:" . $file . "\n";
			}
		}

		$pathDir->close();
	}

	loadReadDir();

	echo memory_get_usage() . "\n";

                   

                   

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn