<?php
namespace framework\src\cmd;
use framework\cao as cao;
final class disk extends cao {
static protected function _mkdir_($dir = ''){
if( !is_dir($dir) || !is_writable($dir) ){
if( !mkdir($dir, 0777, true) ){
return false;
}
}
return is_dir($dir);
}
static protected function _file_size_($path = null){
$data = 0;
if( !empty($path)){
if( is_array($path) ){
foreach($path as $value){
if( !is_file($value) ){
continue;
}
$data += filesize($value);
}
}else
if( is_file($path) ){
$data += filesize($path);
}
}
return $data;
}
static protected function _info_( $dir = '' , $is_all = false ){
$data = array(
'file_size' => 0, 'file_size_unit' => '', 'file_count' => 0, 'dir_count' => 0 );
if( !empty($dir) && is_dir($dir) ){
self::_info_resource( $dir, $data, $is_all );
}
$data['file_size_unit'] = parent::cmd(array($data['file_size']), 'unit storage');
return $data;
}
static private function _info_resource($dir, &$data, $is_all){
if( !$ds = @opendir($dir) ) return;
while( false !== ($file=readdir($ds)) ){
$path = $dir."/".$file if( $file=='.' || $file=='..' ) continue;
if( is_dir($path) ){
$data['dir_count'] ++ if( !empty($is_all) ){
self::_info_resource($path, $data, $is_all) }
}else{
$data['file_count'] ++ $data['file_size'] += filesize($path) }
}
closedir($ds);
}
static protected function _dir_path_( $dir = '' , $is_all = false ){
$data = array();
if( !empty($dir) && is_dir($dir) ){
self::_dir_path_resource( $dir, $data, $is_all );
}
return $data;
}
static private function _dir_path_resource($dir, &$data, $is_all){
if( !$ds = @opendir($dir) ) return;
while( false !== ($file = readdir($ds)) ){
$path = $dir.DIRECTORY_SEPARATOR.$file if( $file == '.' || $file == '..' ) continue;
if( is_dir($path) ){
$data[$path] = $file;
if( !empty($is_all) ){
self::_dir_path_resource($path, $data, $is_all);
}
}
}
closedir($ds);
}
static protected function _dir_delete_( $dir = '' , $is_all = false ){
$data = false;
if( !empty($dir) && is_dir($dir) ){
$data = self::_dir_delete_resource( $dir, $data, $is_all );
}
return $data;
}
static private function _dir_delete_resource($dir, &$data, $is_all){
if( !$ds = @opendir($dir) ) return;
while( false !== ($file = readdir($ds)) ){
$path = $dir.DIRECTORY_SEPARATOR.$file if( $file == '.' || $file == '..' ) continue;
if( is_dir($path) ){
if( !empty($is_all) ){
self::_dir_delete_resource($path, $data, $is_all);
}
}else{
unlink($path);
}
}
closedir($ds);
return rmdir($dir) }
static protected function _file_path_( $dir = '', $is_all = false ){
$data = array();
if( !empty($dir) && is_dir($dir) ){
self::_file_path_resource( $dir, $data, $is_all );
}
return $data;
}
static private function _file_path_resource( $dir, &$data, $is_all ){
if( !$ds = @opendir($dir) ) return;
while( false !== ($file=readdir($ds)) ){
$path = $dir.DIRECTORY_SEPARATOR.$file if( $file=='.' || $file=='..' ) continue;
if( is_dir($path) ){
if( !empty($is_all) ){
self::_file_path_resource($path, $data, $is_all);
}
}else{
$data[$path]= $file }
}
closedir($ds);
}
static protected function _file_delete_( $dir = '', $is_all = false ){
$data = false;
if( !empty($dir) && is_dir($dir) ){
$data = self::_file_delete_resource( $dir, $data, $is_all );
}
return $data;
}
static private function _file_delete_resource( $dir, &$data, $is_all ){
if( !$ds = @opendir($dir) ) return;
while( false !== ($file=readdir($ds)) ){
$path = $dir.DIRECTORY_SEPARATOR.$file if( $file=='.' || $file=='..' ) continue;
if( is_dir($path) ){
if( !empty($is_all) ){
self::_file_delete_resource($path, $data, $is_all);
}
}else{
unlink($path);
}
}
closedir($ds);
return true;
}
}
?>