找回密码
 注册
首页 ≡≡网络技术≡≡ PHP语言 使用php scandir函数遍历文件夹目录和所有文件 ...

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

灰儿 2017-1-30 18:30:59
使用php scandir函数遍历文件夹目录和所有文件
<?php
$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 "<br/>";
}
}
?>


以前的写法:
<?php
/**
* 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;
}
?>


您需要登录后才可以回帖 登录 | 注册
学习中心
站长自定义文字内容,利用碎片时间,随时随地获取优质内容。
Q设计语言 了解更多
Q Design 提供商家设计所需的指导与资源,帮商家快速完成产品设计、降低生产成本。
学习中心
站长自定义文字内容,利用碎片时间,随时随地获取优质内容。
Q设计语言 了解更多
Q Design 提供商家设计所需的指导与资源,帮商家快速完成产品设计、降低生产成本。