最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2016年10月27日 09:19:43   作者:chenpingzhao  
本篇文章主要介紹了PHP實現(xiàn)遞歸目錄的5種方法,主要是利用一些循環(huán)來實現(xiàn)的,感興趣的小伙伴們可以參考一下。

項目開發(fā)中免不了要在服務器上創(chuà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測試運行時間

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

玛曲县| 阳谷县| 昆明市| 诸暨市| 通城县| 利辛县| 黄大仙区| 钟祥市| 德化县| 光山县| 宝丰县| 双城市| 托克逊县| 宜州市| 马边| 平武县| 高淳县| 资中县| 阆中市| 册亨县| 错那县| 民丰县| 高碑店市| 深水埗区| 都兰县| 托克托县| 涞源县| 达州市| 横山县| 柏乡县| 保康县| 老河口市| 阳曲县| 盐边县| 扎鲁特旗| 文安县| 天祝| 虞城县| 大庆市| 鲁甸县| 武城县|