abstract:<?php //目录的创建和读取操作及目录大小 class folder_activity{ //创建目录 function create_folder($filename){ if (!file_exists($filename)){ if(mkdir($filename,0777,true)){ return "目录创建
<?php //目录的创建和读取操作及目录大小 class folder_activity{ //创建目录 function create_folder($filename){ if (!file_exists($filename)){ if(mkdir($filename,0777,true)){ return "目录创建成功!"; }else{ return "目录创建失败!"; } }else{ return "目录已存在,请不要重新创建!"; } } //echo create_folder('fold/practice3'); //读取目录 function read_folder($foldername){ if(file_exists($foldername)){ $array = []; $dir = opendir($foldername); while($item = readdir($dir)){ if($item!='.'&&$item!='..'){ if(is_file($foldername.'/'.$item)){ $array['file'][] = $item; } if(is_dir($foldername.'/'.$item)){ $array['folder'][]= $item; } } } }else{ return "需要读取的目录不存在,请检查目录名是否正确!"; } closedir($dir); return $array; } //var_dump(read_folder('fold')); function dir_size($path){ $sum = 0; global $sum; $dir = opendir($path); while($item = readdir($dir)){ if($item!='.'&&$item!='..'){ if(is_file($path.'/'.$item)){ $sum+=filesize($path.'/'.$item); } if(is_dir($path.'/'.$item)){ $func = __FUNCTION__; $func($path.'/'.$item); } } } //closedir($dir); return $sum; } //echo dir_size('fold'); } ?>
Correcting teacher:天蓬老师Correction time:2019-08-27 09:17:40
Teacher's summary:从php5.3+, spl中封装了许多有用的文件与目录操作类库, 有空了解一下, 可以极大的简化这些操作