disk_free_space()<?php
// $df 包含根目录下可用的字节数
$df = disk_free_space("/");
//在 Windows 下:
disk_free_space("C:");
disk_free_space("D:");
?> http://cn.php.net/manual/zh/function.disk-free-space.php
// $df 包含根目录下可用的字节数
$df = disk_free_space("/");
//在 Windows 下:
disk_free_space("C:");
disk_free_space("D:");
?> http://cn.php.net/manual/zh/function.disk-free-space.php
我的网站目录在/var/html/myweb下
而用户上传的文件在/var/html/myweb/upload下
我想在upload目录的空间使用为1G的时候就不让用户上传了,或者让用户续费再扩大空间
我怎么知道upload的目录大小呢?
//获取文件夹大小
function dir_size($dir)
{
if (!preg_match('#/$#', $dir))
{
$dir .= '/';
}
$totalsize = 0;
//调用文件列表
foreach (get_file_list($dir) as $name)
{
$totalsize += (@is_dir($dir.$name) ? dir_size("$dir$name/") : (int)@filesize($dir.$name));
}
return $totalsize;
}
//获取文件列表
function get_file_list($path)
{
$f = $d = array();
//获取所有文件
foreach (get_all_files($path) as $name)
{ if (@is_dir($path.$name))
{
$d[] = $name;
}
else if (@is_file($path.$name))
{
$f[] = $name;
}
}
natcasesort($d);
natcasesort($f);
return array_merge($d, $f);
}
//获取所有文件
function get_all_files($path)
{
$list = array();
if (($hndl = @opendir($path)) === false)
{
return $list;
}
while (($file=readdir($hndl)) !== false)
{
if ($file != '.' && $file != '..')
{
$list[] = $file;
}
}
closedir($hndl);
return $list;
} //转换单位
function setupSize($fileSize)
{
$size = sprintf("%u", $fileSize);
if($size == 0)
{
return("0 Bytes");
}
$sizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
return round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizename[$i];
}
//目录
$path = './test_dir/';
//显示文件列表
print_r(get_file_list($path)).'<br>';
//显示文件大小
echo dir_size($path).'<br>';
//显示转换过单位的大小
echo setupSize(dir_size($path));
?>
代码包含四个函数,dir_size() 是获取文件夹大小的函数,函数中使用了递归的方法,此函数需要调用 get_file_list() ,获取文件夹中的所有文件列表,如果文件列表中有文件夹存在,就调用 get_all_files() 获取文件夹下的文件列表。由此取得目标文件夹的大小。