循环路径下的所有文件
static function showDir($dirpath)
{
if (!is_dir($dirpath)) {
return false;
}
$handle = scandir($dirpath);
$files = [];
foreach ($handle as $key => $value) {
if ($value == '.' || $value == '..') {
continue;
}
$files[$key]['name'] = $value;
$files[$key]['type'] = filetype($dirpath . $value);
$files[$key]['size'] = self::getFileSize(filesize($dirpath . $value));
$files[$key]['date'] = date("Y/m/d", filectime($dirpath . $value));
$files[$key]['path'] = $dirpath . $value;
//开启递归
// if(is_dir($dirpath.$files[$key]['name'])){
// $files[$key]['child']=ShowDir($files[$key]['path'].'\\');
// };
}
return $files;
}
将获取的文件大小进行转换方便阅读.
static function getFileSize($fileSize)
{
$unit = ['TB' => pow(1024, 4),'GB' => pow(1024, 3),'MB' => pow(1024, 2),'KB' => 1024,'BY'=>1];
$sizeUnit = '';
foreach ($unit as $key => $value) {
if ($fileSize >= $value) {
$sizeUnit = round(($fileSize / $value),0).' '.$key;
break;
}
}
return $sizeUnit;
}