473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Some array counting math

Hi there,

I have a function to create an array of all files in a certain folder,
so i can display the structure.
The actual function is below the message, as is an example of its
output.

As one can see, the filesize is also stored in the array (more info
will be added, like filemtime),
what i need is to sum all bytes per folder.

So if i have e.g.

- folder_1_level1 -size file 1 / 96
* file_1
* file_2

- folder_1_level2 -size file 3, 4, 7, 8, 9
* file_3
* file_4
- folder_1_level3 -size file 7, 8, 9
* file_7
* file_8
* file_9

- folder_2_level2 -size file 5, 6
* file_5
* file_6
The latter should give me the sum of all filesizes in there,
folder_level2 should give me folder_level3's size PLUS other files/
folders that are in folder_level2, and then, folder_level1 should have
the size of all files in there down the entire tree.

I hope it's clear and that someone can help me.

Cheers.
*** FUNCTION CODE ***

function mapit( $dir = '.', $loop = 0, $parent = false ){
$handle = @opendir( $dir );
while( ( $file=readdir( $handle ) ) !== false ){
if( $file != '.' && $file != '..' ){
$point = $dir."/".$file;
$fileSize = filesize( $dir.'/'.$file );
if( is_dir( $point ) )
$info['dirs'][$file] = mapit( $point , $loop+1, $file );
else
$info['files'][$file] = $parent.' -'.$fileSize;
}
}
return $info;
}

$structure = mapit();
print_r( $structure );

*** /FUNCTION CODE ***

*** RESULT EXAMPLE ***

Array
(
[dirs] =Array
(
[upload] =Array
(
[files] =Array
(
[index.htm] =upload -1391
[index.php] =upload -23
[upload.php] =upload -7733
)

[dirs] =Array
(
[img] =Array
(
[files] =Array
(
[0.jpg] =img -36866
[12.jpg] =img -218
[13.jpg] =img -36866
)
)
)
)

[files] =Array
(
[dirs] =Array
(
[just-a-dir] =>
[images] =Array
(
[files] =Array
(
[lion.jpg] =flip ->
350398
)
)
)
[files] =Array
(
[logo.pdf] =files -157764
[tiger.jpg] =files -350398
)
)
)

[files] =Array
(
[ftp.php] = -10238
[cats.php] = -8237
)
)

*** /RESULT EXAMPLE ***
Nov 27 '07 #1
4 1568
On Nov 27, 7:25 pm, frizzle <phpfriz...@gmail.comwrote:
Hi there,

I have a function to create an array of all files in a certain folder,
so i can display the structure.
The actual function is below the message, as is an example of its
output.

As one can see, the filesize is also stored in the array (more info
will be added, like filemtime),
what i need is to sum all bytes per folder.

So if i have e.g.

- folder_1_level1 -size file 1 / 96
* file_1
* file_2

- folder_1_level2 -size file 3, 4, 7, 8, 9
* file_3
* file_4
- folder_1_level3 -size file 7, 8, 9
* file_7
* file_8
* file_9

- folder_2_level2 -size file 5, 6
* file_5
* file_6

The latter should give me the sum of all filesizes in there,
folder_level2 should give me folder_level3's size PLUS other files/
folders that are in folder_level2, and then, folder_level1 should have
the size of all files in there down the entire tree.

I hope it's clear and that someone can help me.

Cheers.

*** FUNCTION CODE ***

function mapit( $dir = '.', $loop = 0, $parent = false ){
$handle = @opendir( $dir );
while( ( $file=readdir( $handle ) ) !== false ){
if( $file != '.' && $file != '..' ){
$point = $dir."/".$file;
$fileSize = filesize( $dir.'/'.$file );
if( is_dir( $point ) )
$info['dirs'][$file] = mapit( $point , $loop+1, $file );
else
$info['files'][$file] = $parent.' -'.$fileSize;
}
}
return $info;

}

$structure = mapit();
print_r( $structure );

*** /FUNCTION CODE ***

*** RESULT EXAMPLE ***

Array
(
[dirs] =Array
(
[upload] =Array
(
[files] =Array
(
[index.htm] =upload -1391
[index.php] =upload -23
[upload.php] =upload -7733
)

[dirs] =Array
(
[img] =Array
(
[files] =Array
(
[0.jpg] =img -36866
[12.jpg] =img -218
[13.jpg] =img -36866
)
)
)
)

[files] =Array
(
[dirs] =Array
(
[just-a-dir] =>
[images] =Array
(
[files] =Array
(
[lion.jpg] =flip ->
350398
)
)
)
[files] =Array
(
[logo.pdf] =files -157764
[tiger.jpg] =files -350398
)
)
)

[files] =Array
(
[ftp.php] = -10238
[cats.php] = -8237
)
)

*** /RESULT EXAMPLE ***
This should work. This function recursively goes through a given array
and adds up the sizes.
Note that I had to use regex to get the numerical size as you have
this string, '->' , in there.

function getSize($map) {
$size = 0;
foreach($map as $m) {
if(!is_array($m)) {
preg_match("/[0-9]{1,20}/", $m, $p); // regex to get the size
$size += $p[0];
} else {
$size += getsize($m);
}
}
return $size;
}

$structure = mapit();
echo getsize($structure['dirs']['upload']);

--
Kailash Nadh | http://kailashnadh.name
Nov 27 '07 #2
On Nov 27, 10:16 pm, Kailash Nadh <kailash.n...@gmail.comwrote:
On Nov 27, 7:25 pm, frizzle <phpfriz...@gmail.comwrote:
Hi there,
I have a function to create an array of all files in a certain folder,
so i can display the structure.
The actual function is below the message, as is an example of its
output.
As one can see, the filesize is also stored in the array (more info
will be added, like filemtime),
what i need is to sum all bytes per folder.
So if i have e.g.
- folder_1_level1 -size file 1 / 96
* file_1
* file_2
- folder_1_level2 -size file 3, 4, 7, 8, 9
* file_3
* file_4
- folder_1_level3 -size file 7, 8, 9
* file_7
* file_8
* file_9
- folder_2_level2 -size file 5, 6
* file_5
* file_6
The latter should give me the sum of all filesizes in there,
folder_level2 should give me folder_level3's size PLUS other files/
folders that are in folder_level2, and then, folder_level1 should have
the size of all files in there down the entire tree.
I hope it's clear and that someone can help me.
Cheers.
*** FUNCTION CODE ***
function mapit( $dir = '.', $loop = 0, $parent = false ){
$handle = @opendir( $dir );
while( ( $file=readdir( $handle ) ) !== false ){
if( $file != '.' && $file != '..' ){
$point = $dir."/".$file;
$fileSize = filesize( $dir.'/'.$file );
if( is_dir( $point ) )
$info['dirs'][$file] = mapit( $point , $loop+1, $file );
else
$info['files'][$file] = $parent.' -'.$fileSize;
}
}
return $info;
}
$structure = mapit();
print_r( $structure );
*** /FUNCTION CODE ***
*** RESULT EXAMPLE ***
Array
(
[dirs] =Array
(
[upload] =Array
(
[files] =Array
(
[index.htm] =upload -1391
[index.php] =upload -23
[upload.php] =upload -7733
)
[dirs] =Array
(
[img] =Array
(
[files] =Array
(
[0.jpg] =img -36866
[12.jpg] =img -218
[13.jpg] =img -36866
)
)
)
)
[files] =Array
(
[dirs] =Array
(
[just-a-dir] =>
[images] =Array
(
[files] =Array
(
[lion.jpg] =flip ->
350398
)
)
)
[files] =Array
(
[logo.pdf] =files -157764
[tiger.jpg] =files -350398
)
)
)
[files] =Array
(
[ftp.php] = -10238
[cats.php] = -8237
)
)
*** /RESULT EXAMPLE ***

This should work. This function recursively goes through a given array
and adds up the sizes.
Note that I had to use regex to get the numerical size as you have
this string, '->' , in there.

function getSize($map) {
$size = 0;
foreach($map as $m) {
if(!is_array($m)) {
preg_match("/[0-9]{1,20}/", $m, $p); // regex to get the size
$size += $p[0];
} else {
$size += getsize($m);
}
}
return $size;

}

$structure = mapit();
echo getsize($structure['dirs']['upload']);

--
Kailash Nadh |http://kailashnadh.name
Thank you very much for your quick help!
Not to be a nag though, but if i store other numerical values in the
array (like filemtime), wouldn't they be counted as well?

Would it be better to give each file a ['size'] key? In the best case,
i thought, it would be best to somehow perform the count at the same
time as doing the first loop, so you wouldn't have to run unnecessary
loop, or am i wrong?

Thanks a lot again though!!

(The '->' part doesn't have to be there, it was merely a visual
"guide" for me ...)
Nov 27 '07 #3
On Nov 27, 9:45 pm, frizzle <phpfriz...@gmail.comwrote:
On Nov 27, 10:16 pm, Kailash Nadh <kailash.n...@gmail.comwrote:
On Nov 27, 7:25 pm, frizzle <phpfriz...@gmail.comwrote:
Hi there,
I have a function to create an array of all files in a certain folder,
so i can display the structure.
The actual function is below the message, as is an example of its
output.
As one can see, the filesize is also stored in the array (more info
will be added, like filemtime),
what i need is to sum all bytes per folder.
So if i have e.g.
- folder_1_level1 -size file 1 / 96
* file_1
* file_2
- folder_1_level2 -size file 3, 4, 7, 8, 9
* file_3
* file_4
- folder_1_level3 -size file 7, 8, 9
* file_7
* file_8
* file_9
- folder_2_level2 -size file 5, 6
* file_5
* file_6
The latter should give me the sum of all filesizes in there,
folder_level2 should give me folder_level3's size PLUS other files/
folders that are in folder_level2, and then, folder_level1 should have
the size of all files in there down the entire tree.
I hope it's clear and that someone can help me.
Cheers.
*** FUNCTION CODE ***
function mapit( $dir = '.', $loop = 0, $parent = false ){
$handle = @opendir( $dir );
while( ( $file=readdir( $handle ) ) !== false ){
if( $file != '.' && $file != '..' ){
$point = $dir."/".$file;
$fileSize = filesize( $dir.'/'.$file );
if( is_dir( $point ) )
$info['dirs'][$file] = mapit( $point , $loop+1, $file );
else
$info['files'][$file] = $parent.' -'.$fileSize;
}
}
return $info;
}
$structure = mapit();
print_r( $structure );
*** /FUNCTION CODE ***
*** RESULT EXAMPLE ***
Array
(
[dirs] =Array
(
[upload] =Array
(
[files] =Array
(
[index.htm] =upload -1391
[index.php] =upload -23
[upload.php] =upload -7733
)
[dirs] =Array
(
[img] =Array
(
[files] =Array
(
[0.jpg] =img -36866
[12.jpg] =img -218
[13.jpg] =img -36866
)
)
)
)
[files] =Array
(
[dirs] =Array
(
[just-a-dir] =>
[images] =Array
(
[files] =Array
(
[lion.jpg] =flip ->
350398
)
)
)
[files] =Array
(
[logo.pdf] =files -157764
[tiger.jpg] =files -350398
)
)
)
[files] =Array
(
[ftp.php] = -10238
[cats.php] = -8237
)
)
*** /RESULT EXAMPLE ***
This should work. This function recursively goes through a given array
and adds up the sizes.
Note that I had to use regex to get the numerical size as you have
this string, '->' , in there.
function getSize($map) {
$size = 0;
foreach($map as $m) {
if(!is_array($m)) {
preg_match("/[0-9]{1,20}/", $m, $p); // regex to get the size
$size += $p[0];
} else {
$size += getsize($m);
}
}
return $size;
}
$structure = mapit();
echo getsize($structure['dirs']['upload']);
--
Kailash Nadh |http://kailashnadh.name

Thank you very much for your quick help!
np ;)
Not to be a nag though, but if i store other numerical values in the
array (like filemtime), wouldn't they be counted as well?

Would it be better to give each file a ['size'] key? In the best case,
i thought, it would be best to somehow perform the count at the same
time as doing the first loop, so you wouldn't have to run unnecessary
loop, or am i wrong?

Thanks a lot again though!!

(The '->' part doesn't have to be there, it was merely a visual
"guide" for me ...)
Yes, extra keys would cause the function not to work.
Like you said, a 'size' key would get rid of all the confusion. If you
get it there, simply get rid of the regex line and add the value of
the 'size' key to $size.

--
Kailash Nadh | http://kailashnadh.name
Nov 27 '07 #4
On Nov 27, 10:51 pm, Kailash Nadh <kailash.n...@gmail.comwrote:
On Nov 27, 9:45 pm, frizzle <phpfriz...@gmail.comwrote:
On Nov 27, 10:16 pm, Kailash Nadh <kailash.n...@gmail.comwrote:
On Nov 27, 7:25 pm, frizzle <phpfriz...@gmail.comwrote:
Hi there,
I have a function to create an array of all files in a certain folder,
so i can display the structure.
The actual function is below the message, as is an example of its
output.
As one can see, the filesize is also stored in the array (more info
will be added, like filemtime),
what i need is to sum all bytes per folder.
So if i have e.g.
- folder_1_level1 -size file 1 / 96
* file_1
* file_2
- folder_1_level2 -size file 3, 4, 7, 8, 9
* file_3
* file_4
- folder_1_level3 -size file 7, 8, 9
* file_7
* file_8
* file_9
- folder_2_level2 -size file 5, 6
* file_5
* file_6
The latter should give me the sum of all filesizes in there,
folder_level2 should give me folder_level3's size PLUS other files/
folders that are in folder_level2, and then, folder_level1 should have
the size of all files in there down the entire tree.
I hope it's clear and that someone can help me.
Cheers.
*** FUNCTION CODE ***
function mapit( $dir = '.', $loop = 0, $parent = false ){
$handle = @opendir( $dir );
while( ( $file=readdir( $handle ) ) !== false ){
if( $file != '.' && $file != '..' ){
$point = $dir."/".$file;
$fileSize = filesize( $dir.'/'.$file );
if( is_dir( $point ) )
$info['dirs'][$file] = mapit( $point , $loop+1, $file );
else
$info['files'][$file] = $parent.' -'.$fileSize;
}
}
return $info;
}
$structure = mapit();
print_r( $structure );
*** /FUNCTION CODE ***
*** RESULT EXAMPLE ***
Array
(
[dirs] =Array
(
[upload] =Array
(
[files] =Array
(
[index.htm] =upload -1391
[index.php] =upload -23
[upload.php] =upload -7733
)
[dirs] =Array
(
[img] =Array
(
[files] =Array
(
[0.jpg] =img -36866
[12.jpg] =img -218
[13.jpg] =img -36866
)
)
)
)
[files] =Array
(
[dirs] =Array
(
[just-a-dir] =>
[images] =Array
(
[files] =Array
(
[lion.jpg] =flip ->
350398
)
)
)
[files] =Array
(
[logo.pdf] =files -157764
[tiger.jpg] =files -350398
)
)
)
[files] =Array
(
[ftp.php] = -10238
[cats.php] = -8237
)
)
*** /RESULT EXAMPLE ***
This should work. This function recursively goes through a given array
and adds up the sizes.
Note that I had to use regex to get the numerical size as you have
this string, '->' , in there.
function getSize($map) {
$size = 0;
foreach($map as $m) {
if(!is_array($m)) {
preg_match("/[0-9]{1,20}/", $m, $p); // regex to get the size
$size += $p[0];
} else {
$size += getsize($m);
}
}
return $size;
}
$structure = mapit();
echo getsize($structure['dirs']['upload']);
--
Kailash Nadh |http://kailashnadh.name
Thank you very much for your quick help!

np ;)
Not to be a nag though, but if i store other numerical values in the
array (like filemtime), wouldn't they be counted as well?
Would it be better to give each file a ['size'] key? In the best case,
i thought, it would be best to somehow perform the count at the same
time as doing the first loop, so you wouldn't have to run unnecessary
loop, or am i wrong?
Thanks a lot again though!!
(The '->' part doesn't have to be there, it was merely a visual
"guide" for me ...)

Yes, extra keys would cause the function not to work.
Like you said, a 'size' key would get rid of all the confusion. If you
get it there, simply get rid of the regex line and add the value of
the 'size' key to $size.

--
Kailash Nadh |http://kailashnadh.name
Great, thanks a lot! I'll probably sadly enough won't have any time
before tomorrow to take a look at it, but i'm confident i can get it
to work! :)

And my remark of doing the counting loop at the same time as the
initial loop, or is that peanuts (in calculating power/speed) for PHP?

Thanks again for helping me so far!
Nov 28 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: effendi | last post by:
I wrote a simple script to remove an element of an array but I don't think this is the best way to go about it. I have a list of about five elements seperated by ";" I split the array using...
6
by: edwardfredriks | last post by:
I'm looking for a script that, instead of counting down, can "count up" from a given date. So the output should be something like "(xx) days since (date/event)" or "(date/event) was (xx) days ago"....
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
3
by: viewsonic | last post by:
Help, im a student that has to write a program for counting coins. Half of the program works but the other half doesn.t should have the following parameters. output is: Name Date total...
2
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the...
4
toxicpaint
by: toxicpaint | last post by:
Hi, can anyone give me a hand. I'm currently displaying 4 random images at the top of a page. I did this using an array of 35 pictures and then writing them to page. The problem I have is that in...
4
by: kidko | last post by:
I'm writing a small game in Javascript (using just PNGs for graphics and DOM manipulation to move them), and most of the content is procedurally generated. This includes all of the weapons involved,...
15
by: DL | last post by:
say, we have the following: // declare an array myArray = ; // short hand? same as myArray = new Array ? // populate it myArray = 0; myArray = 0; myArray = 1; myArray = 0;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.