欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php遍历目录

程序员文章站 2022-06-14 19:09:30
...

1. [PHP]代码

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

	function traversalDir($path = '.')
	{
		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 'Directory ' . $file . ':' . "\n";
                traversalDir($sub_dir);
			}
			else 
			{
				echo 'File in Directory ' . $path . ': ' . $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";
相关标签: php