本文實(shí)例總結(jié)了PHP目錄操作方法。分享給大家供大家參考,具體如下:
目錄操作
新建目錄:mkdir(路徑,權(quán)限,遞歸創(chuàng)建)
刪除目錄:rmdir()
移動(dòng)(改名):rename()
獲取目錄內(nèi)容:
//打開(kāi)目錄
目錄句柄 = opendir()
//讀取目錄
文件名 = readdir(目錄句柄)
依次讀取文件名,同時(shí)向下移動(dòng)文件句柄指針,讀取不到則返回false
//關(guān)閉目錄
closedir()
遞歸讀取目錄內(nèi)容:
?phpshowDir( ../../file function showDir($path,$dep=0){ $pos = opendir($path); while(false!==$file=readdir($pos)){ if($file== . ||$file== .. ) continue; echo str_repeat( nbsp ,$dep*4),$file. /br if(is_dir($path. / .$file)){ $func = __FUNCTION__; $func($path. / .$file,$dep+1);}
運(yùn)行效果圖如下:
?php$res = showDir( ../../file echo pre print_r($res);function showDir($path){ $pos = opendir($path); $next = array(); while(false!==$file=readdir($pos)){ if($file== . ||$file== .. ) continue; $fileinfo = array(); $fileinfo[ name ] = $file; if(is_dir($path. / .$file)){ $fileinfo[ type ] = dir $func = __FUNCTION__; $fileinfo[ next ] = $func($path. / .$file); }else{ $fileinfo[ type ] = file $next[] = $fileinfo; closedir($pos); return $next;}
運(yùn)行效果圖如下:
遞歸刪除目錄:
?phpshowDir( ../../file/sim function showDir($path,$dep=0){ $pos = opendir($path); while(false!==$file=readdir($pos)){ if($file== . ||$file== .. ) continue;// echo str_repeat( nbsp ,$dep*4),$file. /br if(is_dir($path. / .$file)){ $func = __FUNCTION__; $func($path. / .$file,$dep+1); }else{ unlink($path. / .$file); rmdir($path); closedir($pos);}
目錄文件編碼問(wèn)題:
展示時(shí),將操作系統(tǒng)編碼轉(zhuǎn)換為響應(yīng)數(shù)據(jù)編碼
windows為gbk,項(xiàng)目 utf-8
iconv( gbk ,utf-8 ,file);
代碼地址存在中文:需要轉(zhuǎn)換為系統(tǒng)編碼
iconv(utf-8 , gbk ,file);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,更多相關(guān)內(nèi)容請(qǐng)關(guān)注PHP !
相關(guān)推薦:
關(guān)于php_pdo 預(yù)處理語(yǔ)句的解析
關(guān)于PHP的鏈表操作
以上就是關(guān)于PHP的目錄操作的詳細(xì)內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選