You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.3 KiB
97 lines
2.3 KiB
3 years ago
|
<?php
|
||
3 years ago
|
/*
|
||
|
* @Author: witersen
|
||
|
* @Date: 2022-04-24 23:37:05
|
||
|
* @LastEditors: witersen
|
||
3 years ago
|
* @LastEditTime: 2022-04-30 19:26:32
|
||
3 years ago
|
* @Description: QQ:1801168257
|
||
|
*/
|
||
3 years ago
|
|
||
|
/**
|
||
|
* 获取文件夹体积
|
||
3 years ago
|
*
|
||
|
* 使用php的内置方法
|
||
3 years ago
|
*/
|
||
|
function FunGetDirSize($dir)
|
||
3 years ago
|
{
|
||
|
clearstatcache();
|
||
|
$dh = opendir($dir) or exit('打开目录错误'); //打开目录,返回一个目录流
|
||
|
$size = 0; //初始大小为0
|
||
|
while (false !== ($file = @readdir($dh))) { //循环读取目录下的文件
|
||
|
if ($file != '.' and $file != '..') {
|
||
|
$path = $dir . '/' . $file; //设置目录,用于含有子目录的情况
|
||
|
if (is_dir($path)) {
|
||
3 years ago
|
$size += FunGetDirSize($path); //递归调用,计算目录大小
|
||
3 years ago
|
} elseif (is_file($path)) {
|
||
|
$size += filesize($path); //计算文件大小
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
closedir($dh); //关闭目录流
|
||
|
return $size; //返回大小
|
||
|
}
|
||
3 years ago
|
|
||
|
/**
|
||
|
* 传入以Byte为单位的值
|
||
|
* 根据大小返回带有单位的值
|
||
|
*/
|
||
|
function FunFormatSize($size)
|
||
|
{
|
||
|
if ($size == 0) {
|
||
|
return 0;
|
||
|
}
|
||
|
if ($size > 1073741824) {
|
||
|
$size = round($size / 1073741824, 3) . 'GB';
|
||
|
} else if ($size > 1048576) {
|
||
|
$size = round($size / 1048576, 2) . 'MB';
|
||
|
} else if ($size > 1024) {
|
||
|
$size = round($size / 1024, 1) . 'KB';
|
||
|
} else {
|
||
|
$size = $size . 'B';
|
||
|
}
|
||
|
return $size;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取文件夹体积
|
||
|
*
|
||
|
* 使用Linux命令
|
||
|
*
|
||
|
* 返回byte
|
||
|
*/
|
||
|
function FunGetDirSizeDu($path)
|
||
|
{
|
||
|
$cmd = sprintf("du -s '%s' | awk '{print $1}'", $path);
|
||
3 years ago
|
$result = FunShellExec($cmd);
|
||
3 years ago
|
$result = $result['result'];
|
||
3 years ago
|
$result = (int)trim($result) * 1024;
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取文件夹下的文件列表
|
||
|
*
|
||
|
* 文件名
|
||
|
* 文件体积
|
||
|
* 最后修改时间
|
||
|
*/
|
||
|
function FunGetDirFileList($path)
|
||
|
{
|
||
|
$filename = scandir($path);
|
||
|
|
||
|
$fileArray = [];
|
||
|
|
||
|
foreach ($filename as $key => $value) {
|
||
|
if ($value == '.' || $value == '..' || is_dir($path . '/' . $value)) {
|
||
|
continue;
|
||
|
}
|
||
|
array_push($fileArray, [
|
||
|
'fileName' => $value,
|
||
|
'fileSize' => FunFormatSize(filesize($path . '/' . $value)),
|
||
|
'fileEditTime' => date('Y-m-d H:i:s', (int)filemtime($path . '/' . $value))
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
return $fileArray;
|
||
|
}
|