Why can't this program traverse all files and folders in a folder? The following is a program that calculates the size of all files and folders under a folder. I want it to calculate the size of the 'znb1 'folder.
The structure of IS
Znb1/. buildpath,
Znb1/. project,
Znb1/223/s. php,
Znb1/settings/newfile. php,
Znb1/settings/org. eclipse. php. core. prefs
After the program runs, only the sizes of znb1/. buildpath and znb1/. project are calculated.
Why? The software I use is wampserver and editplus as follows:
Function dirsize ($ dirname ){
$ Dir = opendir ($ dirname );
While ($ file = readdir ($ dir ))! = FALSE ){
$ Filename = $ dirname. '\'. $ file;
If ($ file! = '.' & $ File! = '..'){
If (is_dir ($ file )){
$ Count + = dirsize ($ file );
} Else {
$ Count + = filesize ($ filename );
}
}
}
Return $ count;
}
Echo dirsize ('znb1 ');
?>
Reply to discussion (solution)
If (is_dir ($ filename )){
If it is $ file, the relative path will be searched in the current directory. False if not found
Path is required!
If (is_dir ($ filename )){
$ Count + = dirsize ($ filename );
Set_time_limit (0 );
Function dirsize ($ dirname ){
If (is_dir ($ dirname )){
$ Dir = opendir ($ dirname );
If ($ dir! = False ){
While ($ file = readdir ($ dir ))! = FALSE ){
$ Filename = $ dirname. '\'. $ file;
If (is_file ($ filename )){
$ Count [] = filesize ($ filename). "@". $ filename;
} Else if (is_dir ($ filename )){
If ($ file! = "." & $ File! = ".."){
Dirsize ($ filename );
}
}
}
}
Return $ count;
} Else {
Return false;
}
}
$ Path = "D: \ APMServ5.2.6 \ www \ htdocs \ localhost ";
$ C = dirsize ($ path );
Var_dump ($ c );
+ ----------------------------------------------------------------- +
After a modification, we can calculate it. we haven't tested the relative strength! For more information, see!
I will give you a function to traverse folders.
You can refer to: http://www.bacysoft.cn/thread-77-1-1.html