Shark Deng

Interactive Designer @Shanju Design in Shanghai

touched caffe / mxnet / python / wordpress / bootstrap / Arduino / Raspberry Pi / webgl / three.js / PIXI / 3dMax

Email

Github

php递归显示文件目录,生成游戏资源列表

2019-11-19 03:49:07

直接上码!函数已做好,可直接用。

–> getList.php <–


<?php
	
	
	//Shark
	function readDirec($name) {
		$results = array();
		
		$allFiles = scandir($name);
		
		foreach ($allFiles as $f) {
	
			//dotfile
			if(substr($f, 0, 1) == ".") {
				continue;
			}
	
			$path = $name.DIRECTORY_SEPARATOR.$f;
			
			if (is_dir($path)) {
				
				$results[$f] = readDirec($path);
							
			} else if (is_file($path)){
			
				array_push($results, $f);
				
			}

		}

		return $results;
	}
	

	
	function saveList($assetName, $listName){
	
		$r = json_encode(readDirec($assetName), JSON_PRETTY_PRINT);

		file_put_contents($assetName.DIRECTORY_SEPARATOR.$listName, $r);
	
	}
	
	
	saveList("./", "list.json");

?>

 

生成的json效果:

 

>>>>改进版:过滤掉getList.php和list.json<<<<<

<?php

function getContents($fileName){
 
 $results = [];
 
 $files = scandir($fileName);


 foreach ($files as $file) {
 //系统文件 dotfile || 两个附加文件(本php和资源列表)
 if (substr($file, 0, 1) == "." || in_array($file, array("list.json", "getList.php"))) continue;
 
 //$fullPath
 if ($fileName == "./") {
 $fullPath = $fileName . $file ;
 } else {
 $fullPath = $fileName . "/" . $file;
 }

 if (is_dir($fullPath)) {
 $results[$file] = getContents($fullPath);
 
 } else if (is_file($fullPath)){
 array_push($results, $file);
 }
 }
 
 return $results;
 
}

$f = getContents("./"); //本文件夹 
$f = json_encode($f, JSON_PRETTY_PRINT);
file_put_contents("list.json", $f); //如果报权限错误,chmod 777 assets即可

?>

 



——By SharkDeng
如果你喜欢我的文章,欢迎红包赞赏

Leave a Reply

Your email address will not be published. Required fields are marked *