Home > AI > Uncategorized

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

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

–> 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即可

?>

 

Related posts:

Leave a Reply