Mr Bosh
Mr Bosh

Reputation:

php disk_total_space

i need help with disk_total_space function..

i have this on my code

<?php
          $sql="select * from users order by id";
          $result=mysql_query($sql);
          while($row=mysql_fetch_array($result)) {
?>
Name : <?php echo $row['name']; ?>
Email : <?php echo $row['email']; ?>
Diskspace Available : <?php 
$dir = "C:/xampp/htdocs/freehosting/".$row['name'];
disk_total_space($dir);
} ?>

However this return me same disk space for every users ..

Anyone can shed me some light?

thanks :)

Upvotes: 2

Views: 4852

Answers (6)

JPCM
JPCM

Reputation: 1

You could use the function:

function size_directory($ruta)
{
    $gestor = opendir($ruta);
        $total = 0;
        while (($archivo = readdir($gestor)) !== false) {
            $ruta_completa = $ruta . "/" . $archivo;
            if ($archivo != "." && $archivo != "..") {
            $total += filesize($ruta_completa);
            }
        }
    return round($total / 1024, 0);
}

Upvotes: 0

Paolo Bergantino
Paolo Bergantino

Reputation: 488554

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.

Upvotes: 3

Paul Dixon
Paul Dixon

Reputation: 300975

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

Upvotes: 2

Greg
Greg

Reputation: 321766

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();
}

Upvotes: 0

ceejayoz
ceejayoz

Reputation: 180065

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.

Upvotes: 1

Sampson
Sampson

Reputation: 268414

https://www.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.

Upvotes: 5

Related Questions