php scandir 目录树,使用php scandir函数遍历文件夹目录和所有文件

使用php scandir函数遍历文件夹目录和所有文件

$dir = "."; //当前目录

list_file($dir);

function list_file($dir){

$list = scandir($dir); // 得到该文件下的所有文件和文件夹

foreach($list as $file){//遍历

$file_location=$dir."/".$file;//生成路径

if(is_dir($file_location) && $file!="." &&$file!=".."){ //判断是不是文件夹

echo "------------------------sign in $file_location------------------";

list_file($file_location); //继续遍历

}

echo "
";

}

}

?>

以前的写法:

/**

* Get an array that represents directory tree

* @param string $directory Directory path

* @param bool $recursive Include sub directories

* @param bool $listDirs Include directories on listing

* @param bool $listFiles Include files on listing

* @param regex $exclude Exclude paths that matches this regex

*/

function directoryToArray($directory, $recursive = true, $listDirs = false, $listFiles = true, $exclude = '') {

$arrayItems = array();

$skipByExclude = false;

$handle = opendir($directory);

if ($handle) {

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

preg_match("/(^(([\.]){1,2})$|(\.(svn|git|md))|(Thumbs\.db|\.DS_STORE))$/iu", $file, $skip);

if($exclude){

preg_match($exclude, $file, $skipByExclude);

}

if (!$skip && !$skipByExclude) {

if (is_dir($directory. DIRECTORY_SEPARATOR . $file)) {

if($recursive) {

$arrayItems = array_merge($arrayItems, directoryToArray($directory. DIRECTORY_SEPARATOR . $file, $recursive, $listDirs, $listFiles, $exclude));

}

if($listDirs){

$file = $directory . DIRECTORY_SEPARATOR . $file;

$arrayItems[] = $file;

}

} else {

if($listFiles){

$file = $directory . DIRECTORY_SEPARATOR . $file;

$arrayItems[] = $file;

}

}

}

}

closedir($handle);

}

return $arrayItems;

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值