PHP實(shí)現(xiàn)遞歸目錄的5種方法
項(xiàng)目開發(fā)中免不了要在服務(wù)器上創(chuàng)建文件夾,比如上傳圖片時(shí)的目錄,模板解析時(shí)的目錄等。這不當(dāng)前手下的項(xiàng)目就用到了這個(gè),于是總結(jié)了幾個(gè)循環(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測試運(yùn)行時(shí)間
<?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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
laravel實(shí)現(xiàn)按月或天或小時(shí)統(tǒng)計(jì)mysql數(shù)據(jù)的方法
今天小編就為大家分享一篇laravel實(shí)現(xiàn)按月或天或小時(shí)統(tǒng)計(jì)mysql數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10laravel通過創(chuàng)建自定義artisan make命令來新建類文件詳解
Laravel通過Artisan提供了強(qiáng)大的控制臺(tái)命令來處理非瀏覽器業(yè)務(wù)邏輯。下面這篇文章主要給大家介紹了關(guān)于laravel如何通過創(chuàng)建自定義artisan make命令來新建類文件的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08php使用Swoole實(shí)現(xiàn)毫秒級(jí)定時(shí)任務(wù)的方法
這篇文章主要介紹了php使用Swoole實(shí)現(xiàn)毫秒級(jí)定時(shí)任務(wù)的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09php項(xiàng)目接入xxl-job調(diào)度系統(tǒng)的示例詳解
這篇文章主要介紹了php項(xiàng)目接入xxl-job調(diào)度系統(tǒng)的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12php注冊(cè)和登錄界面的實(shí)現(xiàn)案例(推薦)
下面小編就為大家?guī)硪黄猵hp注冊(cè)和登錄界面的實(shí)現(xiàn)案例(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10在Laravel 的 Blade 模版中實(shí)現(xiàn)定義變量
今天小編就為大家分享一篇在Laravel 的 Blade 模版中實(shí)現(xiàn)定義變量,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10