Roughly is not know how many levels of directory below the directory, also do not know how many files, the need to count the size of space, this can be used in albums/database occupancy/network u disk programs.
The general program idea is to use the recursion to calculate how much space the directory occupies, and then the space to occupy the value of the text file, so long as the access to this TXT file to know how much space occupied, do not have frequent access to read disk, save resources. Each time a user uploads a file or deletes a file, it is then counted again. Of course, you can also save the results to the database.
?
/**
* File:fetch User Directory use size
* Author:heiyeluren * Create:2005-9-19 16:20
* Modifed:2005-9-19 16:41
*/
/*** Basic Function ***/
Calculate Directory Size
function Countdirsize (dir)
{
Handle = Opendir (dir);
while (false!== (folderorfile = Readdir (handle)))
{
if (Folderorfile!= "." && folderorfile!= "...")
{
if (Is_dir ("Dir/folderorfile")) {
Sizeresult + = getdirsize ("Dir/folderorfile");
} else {
Sizeresult + = filesize ("Dir/folderorfile");
}
}
}
Closedir (handle);
return sizeresult;
}
Save user File Size
function Savedirsize (userdir)
{
Userdirsize = Countdirsize (Userdir);
if (!FP = fopen (userdir.) /dir_size.txt "," w+ ")") {
Die ("Open file failed");
} else {
Fwrite (FP, dirsize);
}
}
Get the size of the user directory
function Getdirsize (userdir)
{
user = Addslashes (userdir);
Sizefile = Userdir. " /dir_size.txt ";
if (!FP = fopen (Sizefile, "R") {
return 0;
} else {
Dirsize = fread (FP, FileSize (Sizefile));
}
return dirsize;
}
/*** Invocation Instance ***/
user = "Heiyeluren";
UserPath = "./user/". User;
Retrieve the directory size if the user performs a delete or upload file
if (action = = "Upload" action = "Delete") {
Savedirsize (UserPath);
}
Userdirsize = Getdirsize (userpath)/1024;
echo "User:". User;
echo "Occupy space:". userdirsize;
?>