返回封装创建目录方......登陆

封装创建目录方法、读取目录并包含目录大小

肖凌2019-09-06 23:13:2119

//创建目录

function createFloder($floderName){

         if(!file_exists($floderName)){

         if(mkdir($floderName,0777,true)){

                  return '创建目录成功';

         }

         }else{

                  return '目录已存在';

         }

}

//目录读取

function readFloder($floderName){

             if(!file_exists($floderName)){

                          return '文件目录不存在';

             }else{

             $fItem=[];

             $dir=opendir($floderName);

             while ($item=readdir($dir)) {

                         if($item!='.' && $item!='..'){

                                 if(is_file($floderName.'/'.$item)){

                                          $fItem['file'][]=$item;

                                 }

                                 if(is_dir($floderName.'/'.$item)){

                                          $fItem['dir'][]=$item;

                                 }

                         }

             }

             closedir($dir);

             return $fItem;

             }

}

        $item=readFloder('whyan');

        echo '<pre>'.var_export($item,true).'<br>';

//容量大小转换

function tranSize($fsize){

         $KB=1024;//容量转换进制

         $MB=1024*$KB;

         $GB=1024*$MB;

         if($fsize<$KB){

                 $fsize=$fsize.'B';

         }elseif($fsize<$MB){

                  $fsize=round($fsize/$KB,2).'KB';

         }elseif($fsize<$GB){

                  $fsize=round($fsize/$MB,2).'MB';

         }else{

                 $fsize=round($fsize/$GB,2).'GB';

         }

         return $fsize;

}


//目录大小

function floderSize($floderName){

             $sum=0;

             global $sum;

             if(!file_exists($floderName)){

                      return '目录不存在!';

             }

             $dir=opendir($floderName);

             while($item=readdir($dir)){

                     if($item!='.' && $item!='..'){

                              if(is_file($floderName.'/'.$item)){

                                      $sum+=filesize($floderName.'/'.$item);

                                 }

                              if(is_dir($floderName.'/'.$item)){

                                         $fun=__FUNCTION__;

                                         $fun($floderName.'/'.$item);

                                }

                      }

            }

         closedir($dir);

         return tranSize($sum);

}

echo floderSize('upload');


最新手记推荐

• 新闻模块对应模块的代码• 依赖注入小例子• 模板显示用户信息表,并分页显示• 模板继承,模仿php页面• 模拟用户登录验证的案例 依赖注入 Facade

全部回复(0)我要回复

暂无评论~
  • 取消回复发送
  • PHP中文网