php disk_total_space

后端 未结 5 1366
后悔当初
后悔当初 2021-01-21 11:04

i need help with disk_total_space function..

i have this on my code



        
5条回答
  •  礼貌的吻别
    2021-01-21 11:24

    I think what you want is something like this:

    function foldersize($path) {
        $total_size = 0;
        $files = scandir($path);
    
        foreach($files as $t) {
            if (is_dir(rtrim($path, '/') . '/' . $t)) {
                if ($t<>"." && $t<>"..") {
                    $size = foldersize(rtrim($path, '/') . '/' . $t);
                    $total_size += $size;
                }
            } else {
                $size = filesize(rtrim($path, '/') . '/' . $t);
                $total_size += $size;
            }   
        }
        return $total_size;
    }
    
    function format_size($size) {
        $mod = 1024;
    
        $units = explode(' ','B KB MB GB TB PB');
        for ($i = 0; $size > $mod; $i++) {
            $size /= $mod;
        }
    
        return round($size, 2) . ' ' . $units[$i];
    }
    
    $SIZE_LIMIT = 5368709120; // 5 GB
    $sql="select * from users order by id";
    $result=mysql_query($sql);
    while($row=mysql_fetch_array($result)) {
        $disk_used = foldersize("C:/xampp/htdocs/freehosting/".$row['name']);
        $disk_remaining = $SIZE_LIMIT - $disk_used;
        print 'Name: ' . $row['name'] . '
    '; print 'diskspace used: ' . format_size($disk_used) . '
    '; print 'diskspace left: ' . format_size($disk_remaining) . '

    '; }

    edit: the recursive function had a bug in it originally. Now it should also read folders inside the original folders, and folders inside those folders, and so on.

提交回复
热议问题