|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||||
|
|||||
|
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:
Thanks in advance for the info. |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
RE: Grr... array_sum() isn't working!
I've tried it both ways and I still get the same error.
|
|
#4
|
|||
|
|||
|
RE: Grr... array_sum() isn't working!
Oops... (^me)
|
|
#5
|
|||||
|
|||||
|
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:
|
|
#6
|
|||
|
|||
|
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. ;) |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
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 |
|
#9
|
|||||
|
|||||
|
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:
|
|
#10
|
|||
|
|||
|
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))) |
|
#11
|
|||
|
|||
|
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." ; )
|
|
#12
|
|||
|
|||
|
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. |