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

原创2019-09-06 23:13:2119
摘要: //创建目录 function createFloder($floderName){         if(!file_exists($floderName)){         if(mkdir($floderName,0777,

//创建目录

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


批改老师:Peter-Zhu批改时间:2019-09-07 14:33:56
老师总结:下次记得将, 代码写到 代码块中, 提交, 不要直接提交

发布手记

热门词条

第十期线上培训班