tags:

views:

213

answers:

6

hello,

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 :)

+5  A: 

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.

Jonathan Sampson
A: 

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

Paul Dixon
du isn't going to work well on a system with file paths like C:\ - it doesn't exist in Windows.
ceejayoz
It's funny that's he's running PHP on Windows where this won't work - but if you're on *nix, where most PHP runs, it'll help. http://linux.die.net/man/1/du
Tom Ritter
A: 

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.

ceejayoz
A: 

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();
}
Greg
+1  A: 

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.

Paolo Bergantino
A: 

Thanks for help guys..

I used Paolo Bergantino method, however if there is folder inside C:/xampp/htdocs/freehosting/".$row['name'], it won't count the size of folder inside it.

Any help are deeply apprieacted.

Are you sure? The function I used was made specifically to be recursive.
Paolo Bergantino
I fixed the problem with it. Try it now.
Paolo Bergantino
Hi, any chance that you could remove file listing, but lets have only sum of file / dir sizes?
Done. Sorry, had that there while I was testing it.
Paolo Bergantino
Also, I'm not sure if you're aware of this, but you're on a different account than the one you originally posted this question with...
Paolo Bergantino
Hey, no need be sorry, its work great.. thanks for help :)
Hey any idea why the processor usage shoot up too 100% till the script execution is finish ?Can anything be done to optimize it?