Home > php教程 > PHP源码 > php遍历目录

php遍历目录

PHP中文网
Release: 2016-06-01 14:32:44
Original
1101 people have browsed it

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";
Copy after login

                   

                   

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template