码迷,mamicode.com
首页 > Web开发 > 详细

php遍历文件夹及其下所有文件的代码

时间:2015-01-24 17:18:19      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

php实现遍历当前文件夹以及其下所有文件与文件夹的代码,主要是用到了递归,有需要的朋友,可以参考学习下。
代码如下:
‘; 

function getdir($path) 
{ 
if(!is_dir($path)) return; 
$handle = dir($path); 
while($file=$handle->read()) 
{ 
if($file!=‘.‘ && $file!=‘..‘) 
{ 
$path2 = $path.‘/‘.$file; 
if(is_dir($path2)) 
{ 
echo $file."\t"; 
getdir($path2); 
}else 
{ 
echo $file.‘‘; 
} 
} 
} 
} 
getdir($path); 

echo ‘ 
‘; 

function get_dir_scandir($path){ 

$tree = array(); 
foreach(scandir($path) as $single){ 
if($single!=‘.‘ && $single!=‘..‘) 
{ 
$path2 = $path.‘/‘.$single; 
if(is_dir($path2)) 
{ 
echo $single." \r\n"; 
get_dir_scandir($path2); 
}else 
{ 
echo $single." \r\n"; 
} 
} 
} 
} 
get_dir_scandir($path); 

echo ‘ 
‘; 

function get_dir_glob(){ 
$tree = array(); 
foreach(glob(‘./curl/*‘) as $single){ 
echo $single." \r\n"; 
} 
} 
get_dir_glob(); 

echo ‘ 
‘; 
function myscandir($path) 
{ 
if(!is_dir($path)) return; 
foreach(scandir($path) as $file) 
{ 
if($file!=‘.‘ && $file!=‘..‘) 
{ 
$path2= $path.‘/‘.$file; 
if(is_dir($path2)) 
{ 
echo $file; 
myscandir($path2); 
}else 
{ 
echo $file.‘ ‘; 
} 
} 
} 
} 

myscandir($path); 

echo ‘ 
‘; 

function myglob($path) 
{ 
$path_pattern = $path.‘/*‘; 
foreach(glob($path_pattern) as $file) 
{ 
if(is_dir($file)) 
{ 
echo $file; 
myscandir($file); 
}else 
{ 
echo $file.‘ 
‘; 
} 
} 
} 

myglob($path);
?>
文章由http://nk.39.net/shjl/nszx/index.html整理发布

php遍历文件夹及其下所有文件的代码

标签:

原文地址:http://www.cnblogs.com/ldico/p/4246065.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!