Why can't this program traverse all files and folders in a folder?

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.