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

本文档展示了四种不同的遍历文件夹和检索文件的方法:递归函数、传统目录遍历、scandir函数和glob函数。通过实例展示了如何使用PHP在www.jbxue.com网站的文件路径中查找和处理文件,包括目录结构和非目录文件。
摘要由CSDN通过智能技术生成

代码如下:

/**

* 遍历文件夹下所有文件

* site www.jbxue.com

*/

$path = './filepath';

function getfiles($path)

{

if(!is_dir($path)) return;

$handle = opendir($path);

while( false !== ($file = readdir($handle)))

{

if($file != '.' && $file!='..')

{

$path2= $path.'/'.$file;

if(is_dir($path2))

{

echo ' ';

echo $file;

getfiles($path2);

}else

{

echo ' ';

echo $file;

}

}

}

}

print_r( getfiles($path));

echo '


';

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);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值