国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP實現(xiàn)遞歸目錄的5種方法

2024-05-04 23:51:34
字體:
供稿:網(wǎng)友

項目開發(fā)中免不了要在服務(wù)器上創(chuàng)建文件夾,比如上傳圖片時的目錄,模板解析時的目錄等。這不當(dāng)前手下的項目就用到了這個,于是總結(jié)了幾個循環(huán)創(chuàng)建目錄的方法。

方法一:使用glob循環(huán)

<?php//方法一:使用glob循環(huán) function myscandir1($path, &$arr) {   foreach (glob($path) as $file) {    if (is_dir($file)) {      myscandir1($file . '/*', $arr);    } else {       $arr[] = realpath($file);    }  }}?>

方法二:使用dir && read循環(huán)

<?php//方法二:使用dir && read循環(huán)function myscandir2($path, &$arr) {   $dir_handle = dir($path);  while (($file = $dir_handle->read()) !== false) {     $p = realpath($path . '/' . $file);    if ($file != "." && $file != "..") {      $arr[] = $p;    }     if (is_dir($p) && $file != "." && $file != "..") {      myscandir2($p, $arr);    }  }}?> 

方法三:使用opendir && readdir循環(huán)

<?php//方法三:使用opendir && readdir循環(huán)function myscandir3($path, &$arr) {     $dir_handle = opendir($path);  while (($file = readdir($dir_handle)) !== false) {     $p = realpath($path . '/' . $file);    if ($file != "." && $file != "..") {      $arr[] = $p;    }    if (is_dir($p) && $file != "." && $file != "..") {      myscandir3($p, $arr);    }  }} ?>

 方法四:使用scandir循環(huán)
 

<?php//方法四:使用scandir循環(huán)function myscandir4($path, &$arr) {     $dir_handle = scandir($path);  foreach ($dir_handle as $file) {     $p = realpath($path . '/' . $file);    if ($file != "." && $file != "..") {      $arr[] = $p;    }    if (is_dir($p) && $file != "." && $file != "..") {      myscandir4($p, $arr);    }  }} ?>

方法五:使用SPL循環(huán)

 <?php//方法五:使用SPL循環(huán)function myscandir5($path, &$arr) {   $iterator = new DirectoryIterator($path);  foreach ($iterator as $fileinfo) {     $file = $fileinfo->getFilename();    $p = realpath($path . '/' . $file);    if (!$fileinfo->isDot()) {      $arr[] = $p;    }    if ($fileinfo->isDir() && !$fileinfo->isDot()) {      myscandir5($p, $arr);    }  }}?> 

 可以用xdebug測試運行時間

<?phpmyscandir1('./Code',$arr1);//0.164010047913 myscandir2('./Code',$arr2);//0.243014097214 myscandir3('./Code',$arr3);//0.233012914658 myscandir4('./Code',$arr4);//0.240014076233myscandir5('./Code',$arr5);//0.329999923706  //需要安裝xdebugecho xdebug_time_index(), "/n";?>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 浠水县| 潮安县| 柳林县| 洱源县| 徐水县| 马边| 米脂县| 治县。| 新源县| 兖州市| 明水县| 深州市| 九寨沟县| 德阳市| 寿光市| 台中市| 新兴县| 新余市| 交城县| 香格里拉县| 什邡市| 新乡市| 左云县| 家居| 城市| 绵竹市| 枣庄市| 尼玛县| 塔河县| 云浮市| 乐东| 德州市| 股票| 吴川市| 石景山区| 密云县| 永善县| 东至县| 化隆| 乐平市| 江油市|