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

php遍历目录

PHP中文网
PHP中文网原创
2016-06-01 14:32:441090浏览

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