Home  >  Article  >  php教程  >  获取文件夹下文件列表_在别人的函数的基础上做了优化

获取文件夹下文件列表_在别人的函数的基础上做了优化

PHP中文网
PHP中文网Original
2016-05-25 17:12:12920browse

php代码

/* * * * * * * * *  
 * 列出文件夹下内容,返回数组 $dirArray['dir']:存文件夹;$dirArray['file']:存文件
 * 刘先忠 2012-1-20
 * * * * * * * * */
function getDirs($dir) {
	$dirArray [][] = NULL;
	if (false != ($handle = opendir ( $dir ))) {
		$i = 0;
		$j = 0;
		while ( false !== ($file = readdir ( $handle )) ) {
			if (is_dir ( $dir . $file )) { //判断是否文件夹
				$dirArray ['dir'] [$i] = $file;
				$i ++;
			} else {
				$dirArray ['file'] [$j] = $file;
				$j ++;
			}
		}
		closedir ( $handle );
	}
	return $dirArray;
}
$mydir = getDirs ( "./" );
print_r ( $mydir ['dir'] );
echo "
";
print_r ( $mydir ['file'] );

Statement:
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