php disk_total_space

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

i need help with disk_total_space function..

i have this on my code



        
相关标签:
5条回答
  • 2021-01-21 11:21

    The comments on the function on PHP.net appear to indicate that this gives the space of the drive/partition $dir is in, not the size of $dir itself.

    0 讨论(0)
  • 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'] . '<br>';
        print 'diskspace used: ' . format_size($disk_used) . '<br>';
        print 'diskspace left: ' . format_size($disk_remaining) . '<br><hr>';
    }
    

    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.

    0 讨论(0)
  • 2021-01-21 11:25

    I think that method will only tell you information about the filesystem or partition the given directory is on. You could try simply shelling out to du though:

    $space_used=`du -sh $dir`;
    

    -s summarizes the entire dir, -h returns the result in "human" units like MB and GB.

    Edit: apologies, I missed that this was on WIndows. WIll leave the answer in case it helps someone searching for a similar problem. For windows, try this suggestion in the PHP manual

    0 讨论(0)
  • 2021-01-21 11:26

    http://us2.php.net/disk_total_space says,

    "Given a string containing a directory, this function will return the total number of bytes on the corresponding filesystem or disk partition."

    You're likely seeing the total_space of C:

    Alternative solutions do exist for both Windows and Linux.

    0 讨论(0)
  • 2021-01-21 11:29

    According to the docs for disk_total_space(), the value returned is for the filesystem the directory is located on. It doesn't count the space used in a directory + its subdirectories.

    You could shell out to du or for a more portable solution:

    $total = 0;
    
    foreach (new RecursiveDirectoryIterator($dir) as $entry)
    {
        if ($entry->isFile())
            $total += $entry->getSize();
    }
    
    0 讨论(0)
提交回复
热议问题