problem : how to calculate disk space

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
graziano
Forum Newbie
Posts: 9
Joined: Sat Dec 14, 2002 7:16 am

problem : how to calculate disk space

Post by graziano »

Hello,

which is the best way to calculate disk usage on a directory (and its subdirectory) using php without receing errors if a not readable dir is found ?

I tried to use @shell_exec() and @passthru with the linux command du , but if "du" find some not readable directory it reports error (and there is no way to hide it )

Code: Select all

<?
ob_start();
@passthru("/usr/bin/du -sk /home/test");
$spacea = ob_get_contents();
ob_end_clean();
$spaceb = ereg_replace ("/home/test", "", $spacea);
$spaceb = str_replace(' ','',$spaceb);
$spacec = $spaceb/1024;
$spaced = number_format($spacec, 3, ',', ' ');
?>



Thanks
Graz
kcomer
Forum Contributor
Posts: 108
Joined: Tue Aug 27, 2002 8:50 am

Post by kcomer »

I've had good luck using php's file system functions for this stuff. Haven't used it to calucltae the file sizes but there is a filsize function which should do the trick. Using readdir and readfile along with filesize you can make a script that reads all the files and folders and gets the sizes of each file, then just store the info in an array and do some calculations on it.
Post Reply