SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Non-Member bronze trophy geniusgoalie's Avatar
    Join Date
    Sep 2004
    Location
    Buffalo, USA
    Posts
    979
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why is this function only working for one level?

    I can't figure out what is wrong- why won't this function find the size of all directories under the one directory, not just all files in that directory? Can anyone see why this isnt working?
    PHP Code:
    function dirsize($dirName '.') {
       
    $dir  dir($dirName);
       
    $size 0;

       while(
    $file $dir->read()) {
           if (
    $file != '.' && $file != '..') {
               if (
    is_dir($file)) {
                   
    $size += dirsize($dirName '/' $file);
               } else {
                   
    $size += filesize($dirName '/' $file);
               }
           }
       }
       
    $dir->close();
       return 
    $size;


  2. #2
    Romans 12:2 codyrockx's Avatar
    Join Date
    Jul 2003
    Location
    Newberg, Oregon
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seemed to work fine when I tested it on my server.
    Codyrobert.com - Designer and Developer

  3. #3
    Non-Member bronze trophy geniusgoalie's Avatar
    Join Date
    Sep 2004
    Location
    Buffalo, USA
    Posts
    979
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works fine- but only for the files in that directory. It should go through every folder every level of the directory- that is the issue.

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    naperville
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geniusgoalie
    I can't figure out what is wrong- why won't this function find the size of all directories under the one directory, not just all files in that directory? Can anyone see why this isnt working?
    PHP Code:
    function dirsize($dirName '.') {
       
    $dir  dir($dirName);
       
    $size 0;

       while(
    $file $dir->read()) {
           if (
    $file != '.' && $file != '..') {
               if (
    is_dir($file)) {
                   
    $size += dirsize($dirName '/' $file);
               } else {
                   
    $size += filesize($dirName '/' $file);
               }
           }
       }
       
    $dir->close();
       return 
    $size;

    Thats just how files work. Make it recursive and have it crawl through all folders to get a "real" size.

  5. #5
    Non-Member bronze trophy geniusgoalie's Avatar
    Join Date
    Sep 2004
    Location
    Buffalo, USA
    Posts
    979
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Super Phil
    Thats just how files work. Make it recursive and have it crawl through all folders to get a "real" size.
    That's the problem! I thought I made it recursive, but apparently I didn't. Can someone give me a link to show me how to make this recursive?

  6. #6
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    naperville
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    function dirsize($dirName '.') {
       
    $dir  dir($dirName);
       
    $size 0;

       while(
    $file $dir->read()) {
           if (
    $file != '.' && $file != '..') {
               if (
    is_dir($file)) {
                   
    $size += dirsize($dirName '/' $file);
               } else {
                   
    $size += filesize($dirName '/' $file);
                   print 
    $dirName '/' $file '<br />';
               }
           }
       }
       
    $dir->close();
       return 
    $size;
    }

    print 
    '<strong>' dirsize();
    ?>
    Same as what you have, with a couple debugging prints; works fine for me. Are you sure your server is 'ok'?

  7. #7
    Non-Member bronze trophy geniusgoalie's Avatar
    Join Date
    Sep 2004
    Location
    Buffalo, USA
    Posts
    979
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oops, I was calling the function wrong! How stupid of me!
    Thanks phil


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •