PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old May 31st, 2002, 06:18 AM
TheAltKey TheAltKey is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 5 TheAltKey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Grr... array_sum() isn't working!

I get a repeating error which says "The argument to array_sum() should be an array". Is this somehow not an array? Arrrgh, what am I doing wrong?

php Code:
Original - php Code
  1.  
  2. <?php
  3.  
  4. class DirectoryInfo {
  5.  
  6. function get_dir_info($dir) {
  7.  
  8. $handle = opendir($dir);
  9. while( $file = readdir($handle) ) {
  10.  
  11.     $d = "0";
  12.     $f = "0";
  13.  
  14.     if($file != '.' and $file != '..') {
  15.         if (is_dir("$dir/$file")) {
  16.             $this->get_dir_info("$dir/$file");
  17.             $dir_array[] = "$dir/$file";
  18.         } else {
  19.             $file_array[] = "$dir/$file";
  20.         }
  21.  
  22.     }
  23.     $totalfiles = array_sum("$file_array");
  24.     $totaldirs = array_sum("$dir_array");
  25. }
  26. echo "$totalfiles files in $totaldirs directories<BR>n";
  27.  
  28. }
  29.  
  30. }
  31.  
  32. $d = new DirectoryInfo;
  33.  
  34. echo $d->get_dir_info("./");
  35.  
  36. ?>


Thanks in advance for the info.

Reply With Quote
  #2  
Old May 31st, 2002, 06:44 AM
Zoombie Zoombie is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Boston, MA, US
Posts: 19 Zoombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Zoombie Send a message via AIM to Zoombie
RE: Grr... array_sum() isn't working!

When you put a variable's name in quotes, it is automatically converted to a string. Look on php.net for info about type-juggling: http://www.php.net/manual/en/language.types.type-juggling.php
Basically, don't put quotes around a variable's name unless you want to convert it to a string.

Reply With Quote
  #3  
Old May 31st, 2002, 07:10 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: Grr... array_sum() isn't working!

I've tried it both ways and I still get the same error.

Reply With Quote
  #4  
Old May 31st, 2002, 07:13 AM
TheAltKey TheAltKey is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 5 TheAltKey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Grr... array_sum() isn't working!

Oops... (^me)

Reply With Quote
  #5  
Old May 31st, 2002, 09:09 AM
Zoombie Zoombie is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Boston, MA, US
Posts: 19 Zoombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Zoombie Send a message via AIM to Zoombie
RE: Grr... array_sum() isn't working!

I'm not sure what you're trying to do - are you trying to get the number of files and directories in a given path? If so, this code might help:
php Code:
Original - php Code
  1.  
  2. <?php
  3. function getfilecount($path){
  4.     $handle = opendir($path);
  5.     while($file = readdir($handle)){
  6.         if(is_dir("$path/$file")){
  7.             $filecount++;
  8.         }else{
  9.             $dircount++;
  10.         }
  11.     }
  12.     closedir($handle);
  13.     return array($filecount,$dircount);
  14. }
  15.  
  16. list($filecount,$dircount) = getfilecount('./');
  17. echo "Found $filecount files and $dircount directories.";
  18. ?>

Reply With Quote
  #6  
Old May 31st, 2002, 11:46 AM
TheAltKey TheAltKey is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 5 TheAltKey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Grr... array_sum() isn't working!

That's basically what I would like the script to do, but I want it to work recursively and count all files in all subfolders as well. I'm not sure how to do something like this though. Any ideas?

P.S. My thanks anyway for taking the time to help me out and post that code. ;)

Reply With Quote
  #7  
Old May 31st, 2002, 06:50 PM
notepad notepad is offline
Codewalkers Loyal (3000 - 3499 posts)
 
Join Date: Apr 2007
Location: Central, IL USA
Posts: 3,214 notepad User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via AIM to notepad
RE: Grr... array_sum() isn't working!

i got some code that displays the contents of a directory and sub directory's along with the file sizes and adds them together.. i can post it here when i get home tonight.

i wrote it cause i host some peoples on my web server and needed to make sure they weren't exceeding their allowed space limit :-) here is an example if yer interested

Reply With Quote
  #8  
Old May 31st, 2002, 07:13 PM
noda noda is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Montreal, Quebec, Canada
Posts: 20 noda User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to noda
RE: Grr... array_sum() isn't working!

There are quite a few things wrong...

1. As mentioned above, don't put quotes around the variable.
2. Change while ($file = readdir($handle)) to while (false !== ($file = readdir($handle))), as specified at php.net/readdir
3. $d = "0"; $f = "0" will do nothing.
4. Your function doesn't return anything, so echoing it will be useless.
5. It's always a good idea if you're using $array[] to put the line "$array = array();" before. This will get rid of errors when the directory is empty.
6. Set $totalfiles and $totaldirs outside the while() loop, so they only need to be set once instead of once per file.
7. Your recursion will not do what you expect it to do. In a moderately-sized tree, you'll get 50 lines of output.

Hope this helps

Reply With Quote
  #9  
Old June 1st, 2002, 05:36 AM
Zoombie Zoombie is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Boston, MA, US
Posts: 19 Zoombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Zoombie Send a message via AIM to Zoombie
RE: Grr... array_sum() isn't working!

To display the files in all directories and subdirectories, you need to make a recursive function:
php Code:
Original - php Code
  1.  
  2. <?php
  3. function getfilecount($path){
  4.     if(substr($path,-1) != '/'){
  5.         $path .= '/';
  6.     }
  7.     $handle = opendir($path);
  8.     while($file = readdir($path)){
  9.         if(is_dir("$path/$file")){
  10.             list($subdircount,$subfilecount) = getfilecount("$path/$file/");
  11.             $dircount += $subdircount+1;
  12.             $filecount += $subfilecount;
  13.         }else{
  14.             $filecount++;
  15.         }
  16.     }
  17.     return array($dircount,$filecount);
  18. }
  19. list($dircount,$filecount) = getfilecount('./');
  20. echo "Found $filecount files in $dircount directories.";
  21. ?>

Reply With Quote
  #10  
Old June 1st, 2002, 06:16 AM
noda noda is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Montreal, Quebec, Canada
Posts: 20 noda User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to noda
RE: Grr... array_sum() isn't working!

Name a file "0" and your function won't work .

As it says on php.net/readdir, change
while ($file = readdir($dir))
to
while (false !== ($file = readdir($dir)))


Reply With Quote
  #11  
Old June 2nd, 2002, 05:45 AM
Zoombie Zoombie is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Boston, MA, US
Posts: 19 Zoombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Zoombie Send a message via AIM to Zoombie
RE: Grr... array_sum() isn't working!

That's right...I never remember that little tidbit - I've never run into a file named "0." ; )

Reply With Quote
  #12  
Old June 2nd, 2002, 10:27 AM
TheAltKey TheAltKey is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 5 TheAltKey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Grr... array_sum() isn't working!

Okay, I did some messing around and I decided to completely rewrite the code from scratch. Now I can get the number of files and directories, but the recursion doesn't seem to be working. In what version of PHP was this enabled? I'm using 4.0.0 and 4.