| 
	
 | 
 Posted by frizzle on 11/27/07 21:45 
On Nov 27, 10:16 pm, Kailash Nadh <kailash.n...@gmail.com> wrote: 
> On Nov 27, 7:25 pm, frizzle <phpfriz...@gmail.com> wrote: 
> 
> 
> 
> > 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 ...)
 
  
Navigation:
[Reply to this message] 
 |