课程名称:WEB在线文件管理器
课程章节:第2章 PHP文件夹操作
主讲老师:king
课程内容:
今天学习的内容包括:
2-1 查看文件夹并递归获取大小
2-2 获取文件夹信息
2-3 dirname()函数实现返回上一级操作
课程收获:
遍历目录函数的封装 得到文件夹大小函数的封装
/**
* 遍历目录函数,只读取目录中的最外层的内容
* @param string $path
* @return array
*/
function readDirectory($path) {
$handle = opendir ( $path );
while ( ($item = readdir ( $handle )) !== false ) {
//.和..这2个特殊目录
if ($item != "." && $item != "..") {
if (is_file ( $path . "/" . $item )) {
$arr ['file'] [] = $item;
}
if (is_dir ( $path . "/" . $item )) {
$arr ['dir'] [] = $item;
}
}
}
closedir ( $handle );
return $arr;
}
//$path="file";
//print_r(readDirectory($path));
/**
* 得到文件夹大小
* @param string $path
* @return int
*/
function dirSize($path){
$sum=0;
global $sum;
$handle=opendir($path);
while(($item=readdir($handle))!==false){
if($item!="."&&$item!=".."){
if(is_file($path."/".$item)){
$sum+=filesize($path."/".$item);
}
if(is_dir($path."/".$item)){
$func=__FUNCTION__;
$func($path."/".$item);
}
}
}
closedir($handle);
return $sum;
}
//$path="file";
//echo dirSize($path);