文件的递归与删除
遞歸
<?php $path="E:/php";function readdiguo($path,$deep=0) {$dir_handle=opendir($path);while(false!==$file=readdir($dir_handle)){if($file=='.' || $file=='..')continue;//輸出該文件echo str_repeat(' ',$deep*4).$file."<br />";//判斷當前是否是目錄if(is_dir($path.'./'.$file)){//是目錄$func_name=__FUNCTION__;//得到當前函數名$func_name($path.'./'.$file,$deep+1);}} } readdiguo($path); ?>遞歸到數組
<?php $path="E:/php"; $list=readdirQiantao($path); foreach ($list as $first_key=>$first) {echo $first_key."=".$first["name"]."<br />";if($first["type"]=='file')continue;foreach ($first["nested"] as $two_key=>$two){echo " ".$two_key."=".$two["name"]."<br />";} } function readdirQiantao($path) {$nested=array();//存儲當前目錄下文件$dir_handle=opendir($path);while(false!=$file=readdir($dir_handle)){if($file=='.'||$file=="..")continue;//創建當前文件信息$fileinfo=array();$fileinfo["name"]=$file;//判斷是否為目錄if(is_dir($path.'/'.$file)){//是目錄$fileinfo["type"]="dir";$func_name=__FUNCTION__;$fileinfo["nested"]=$func_name($path."/".$file);}else{//是文件$fileinfo["type"]="file";}//存入數組$nested[]=$fileinfo;}closedir($dir_handle);return $nested;} ?>遞歸刪除文件
<?php $path="./pp"; var_dump(reDirs($path)); function reDirs($path) {$dir_handle=opendir($path);while(false!=$file=readdir($dir_handle)){if($file=='.'||$file=='..')continue;//判斷當前是否目錄if(is_dir($path.'/'.$file)){//是目錄$func_name=__FUNCTION__;$func_name($path.'/'.$file);}else{//是文件unlink($path.'/'.$file);}}closedir($dir_handle);return rmdir($path);//刪除目錄 } ?>?
轉載于:https://www.cnblogs.com/zywf/p/5125771.html
總結
- 上一篇: iOS 整体架构
- 下一篇: 【filezilla】 ubuntu下安