dplinnane
asked on
place all files in a directory in array.
I have the following function it prints out all my files in each directory. How can I place the files in one array.
Every time the directory changes the counter $i is reset to zero so the previous array is written over.
I want to be able to place each variable in its own array.
$file_mod_time[$i]
$win_file_dir[$i]
so that I can insert it into a database.
Any suggestions.
Thanks.
function util_file_dir_stats ($path)
{
if ($dir=@opendir($path))
{//OUTER IF
while (($dir_files=readdir($dir) )!== false)
{//START WHILE
if (is_dir($path.$dir_files) AND $dir_files!= "." AND $dir_files!= "..")
{
//Places all Directory names in $recurse_dir
if ($dir_files == strtolower($dir_files))
{
$i++;
$recurse_dir[$i] = util_file_dir_stats($path. $dir_files );
}
}
elseif($dir_files!= "." AND $dir_files!= "..")
{//IF 1
if ((preg_match("/.php/",$dir _files,$ar ray) ))
{
$i++;
$recurse_dir[$i] = $dir_files;
$win_file_dir[$i] = "$path/$recurse_dir[$i]";
$win_file_dir[$i] = str_replace('//','/',$win_ file_dir[$ i]);
$file_mod_time[$i] = filemtime($win_file_dir[$i ]);
print "$file_mod_time[$i] $win_file_dir[$i] ".'<BR>';
}
}//END IF 1
}//END WHILE
closedir($dir);
}//END OUTER IF
return($array);
}
Every time the directory changes the counter $i is reset to zero so the previous array is written over.
I want to be able to place each variable in its own array.
$file_mod_time[$i]
$win_file_dir[$i]
so that I can insert it into a database.
Any suggestions.
Thanks.
function util_file_dir_stats ($path)
{
if ($dir=@opendir($path))
{//OUTER IF
while (($dir_files=readdir($dir)
{//START WHILE
if (is_dir($path.$dir_files) AND $dir_files!= "." AND $dir_files!= "..")
{
//Places all Directory names in $recurse_dir
if ($dir_files == strtolower($dir_files))
{
$i++;
$recurse_dir[$i] = util_file_dir_stats($path.
}
}
elseif($dir_files!= "." AND $dir_files!= "..")
{//IF 1
if ((preg_match("/.php/",$dir
{
$i++;
$recurse_dir[$i] = $dir_files;
$win_file_dir[$i] = "$path/$recurse_dir[$i]";
$win_file_dir[$i] = str_replace('//','/',$win_
$file_mod_time[$i] = filemtime($win_file_dir[$i
print "$file_mod_time[$i] $win_file_dir[$i] ".'<BR>';
}
}//END IF 1
}//END WHILE
closedir($dir);
}//END OUTER IF
return($array);
}
ASKER
I had tried array_push but could not get it to work as reuired.
I tried your code after some modifications.
I added array_push to the code as follows
The print statement does nothing.
I added print_r outside the while loop.
You can see results below to fully understand.
function util_file_dir_stats ($path)
{
$win_file_dir = array();
$file_mod_time = array();
if ($dir=@opendir($path))
{//OUTER IF
while (($dir_files=readdir($dir) )!== false)
{//START WHILE
if (is_dir($path.$dir_files) AND $dir_files!= "." AND $dir_files!= "..")
{//INNER IF
//Places all Directory names in $recurse_dir
if ($dir_files == strtolower($dir_files))
{//INNER IF 2
$i++;
$recurse_dir[$i] = util_file_dir_stats($path. $dir_files );
}//END INNER IF 2
}//END INNER IF
elseif($dir_files!= "." AND $dir_files!= "..")
{//IF 1
if ((preg_match("/.php/",$dir _files,$ar ray) ))
{
$i++;
$recurse_dir[$i] = $dir_files;
$full_path[$i] = "$path".'/'."$recurse_dir[ $i]";
$tmp[$i] = str_replace('//','/',$full _path[$i]) ;
array_push($win_file_dir, $tmp[$i]);
array_push($file_mod_time, filemtime($tmp[$i]));
//$ct_file_mod_time = sizeof($file_mod_time)-1;
print "$file_mod_time(sizeof($fi le_mod_tim e)-1) $win_file_dir(count($win_f ile_dir)-1 ) ".'<BR>';
}
}//END IF 1
}//END WHILE
print '<pre>';
print_r ($file_mod_time);
print '</pre>';
closedir($dir);
}//END OUTER IF
return($array);
}
The print statement gives the following
print "$file_mod_time(sizeof($fi le_mod_tim e)-1) $win_file_dir(count($win_f ile_dir)-1 ) ".'<BR>';
There are x num directories the space represents the beginigng of the directory. I have shown 5.
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
The print_r statement outputs all arrays.
print '<pre>';
print_r ($file_mod_time);
print '</pre>';
The array keeps resetting to zero when it encounters a new directory.
ANY OTHER IDEAS?
Array
(
[0] => 1062869576
)
Array
(
[0] => 1062971716
[1] => 1062955151
[2] => 1063160482
[3] => 1063211238
[4] => 1063211238
[5] => 1063211238
[6] => 1063211238
[7] => 1063211238
[8] => 1063211238
[9] => 1062955151
[10] => 1063211238
[11] => 1063211238
[12] => 1063035690
[13] => 1062967571
[14] => 1062967571
[15] => 1062967571
[16] => 1062966724
[17] => 1063047313
[18] => 1062971716
[19] => 1062971716
[20] => 1062955151
[21] => 1062966696
[22] => 1062955151
[23] => 1063134390
[24] => 1062971716
[25] => 1062971716
[26] => 1062954938
[27] => 1063229953
[28] => 1062955151
[29] => 1062955151
[30] => 1062955151
[31] => 1062867047
[32] => 1062971716
[33] => 1063031209
[34] => 1062954938
[35] => 1063134390
[36] => 1063066024
[37] => 1062954938
[38] => 1063050897
[39] => 1062971716
[40] => 1063031209
[41] => 1063035690
[42] => 1062971716
[43] => 1063112357
[44] => 1063112357
[45] => 1062967622
[46] => 1062967517
[47] => 1063160520
[48] => 1063112357
[49] => 1062967571
[50] => 1062967017
[51] => 1062955151
[52] => 1063229882
[53] => 1063066094
[54] => 1063160509
)
Array
(
[0] => 1063323061
[1] => 1061215285
[2] => 1062869794
[3] => 1062869964
[4] => 1062869997
)
Array
(
[0] => 1063216146
[1] => 1063229219
[2] => 1063216146
[3] => 1063216146
[4] => 1063216146
[5] => 1063255483
[6] => 1063229219
[7] => 1063229480
[8] => 1063216146
[9] => 1063216146
[10] => 1063216146
)
Array
(
)
Array
(
[0] => 1063034516
)
Array
(
[0] => 1063244860
[1] => 1063223237
[2] => 1063216146
[3] => 1063216146
[4] => 1063216146
[5] => 1063226279
[6] => 1063216146
[7] => 1063216146
[8] => 1063228999
)
Array
(
[0] => 1063034516
[1] => 1061227588
[2] => 1063287856
)
Array
(
)
Array
(
[0] => 1062987010
[1] => 1063161402
[2] => 1062986301
)
Array
(
[0] => 1063251980
[1] => 1063034517
)
Array
(
[0] => 1063252588
[1] => 1063211239
[2] => 1062954966
[3] => 1063036204
[4] => 1062954966
[5] => 1063037138
[6] => 1063234393
)
ETC ETC
I tried your code after some modifications.
I added array_push to the code as follows
The print statement does nothing.
I added print_r outside the while loop.
You can see results below to fully understand.
function util_file_dir_stats ($path)
{
$win_file_dir = array();
$file_mod_time = array();
if ($dir=@opendir($path))
{//OUTER IF
while (($dir_files=readdir($dir)
{//START WHILE
if (is_dir($path.$dir_files) AND $dir_files!= "." AND $dir_files!= "..")
{//INNER IF
//Places all Directory names in $recurse_dir
if ($dir_files == strtolower($dir_files))
{//INNER IF 2
$i++;
$recurse_dir[$i] = util_file_dir_stats($path.
}//END INNER IF 2
}//END INNER IF
elseif($dir_files!= "." AND $dir_files!= "..")
{//IF 1
if ((preg_match("/.php/",$dir
{
$i++;
$recurse_dir[$i] = $dir_files;
$full_path[$i] = "$path".'/'."$recurse_dir[
$tmp[$i] = str_replace('//','/',$full
array_push($win_file_dir, $tmp[$i]);
array_push($file_mod_time,
//$ct_file_mod_time = sizeof($file_mod_time)-1;
print "$file_mod_time(sizeof($fi
}
}//END IF 1
}//END WHILE
print '<pre>';
print_r ($file_mod_time);
print '</pre>';
closedir($dir);
}//END OUTER IF
return($array);
}
The print statement gives the following
print "$file_mod_time(sizeof($fi
There are x num directories the space represents the beginigng of the directory. I have shown 5.
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
Array(sizeof(Array)-1) Array(count(Array)-1)
The print_r statement outputs all arrays.
print '<pre>';
print_r ($file_mod_time);
print '</pre>';
The array keeps resetting to zero when it encounters a new directory.
ANY OTHER IDEAS?
Array
(
[0] => 1062869576
)
Array
(
[0] => 1062971716
[1] => 1062955151
[2] => 1063160482
[3] => 1063211238
[4] => 1063211238
[5] => 1063211238
[6] => 1063211238
[7] => 1063211238
[8] => 1063211238
[9] => 1062955151
[10] => 1063211238
[11] => 1063211238
[12] => 1063035690
[13] => 1062967571
[14] => 1062967571
[15] => 1062967571
[16] => 1062966724
[17] => 1063047313
[18] => 1062971716
[19] => 1062971716
[20] => 1062955151
[21] => 1062966696
[22] => 1062955151
[23] => 1063134390
[24] => 1062971716
[25] => 1062971716
[26] => 1062954938
[27] => 1063229953
[28] => 1062955151
[29] => 1062955151
[30] => 1062955151
[31] => 1062867047
[32] => 1062971716
[33] => 1063031209
[34] => 1062954938
[35] => 1063134390
[36] => 1063066024
[37] => 1062954938
[38] => 1063050897
[39] => 1062971716
[40] => 1063031209
[41] => 1063035690
[42] => 1062971716
[43] => 1063112357
[44] => 1063112357
[45] => 1062967622
[46] => 1062967517
[47] => 1063160520
[48] => 1063112357
[49] => 1062967571
[50] => 1062967017
[51] => 1062955151
[52] => 1063229882
[53] => 1063066094
[54] => 1063160509
)
Array
(
[0] => 1063323061
[1] => 1061215285
[2] => 1062869794
[3] => 1062869964
[4] => 1062869997
)
Array
(
[0] => 1063216146
[1] => 1063229219
[2] => 1063216146
[3] => 1063216146
[4] => 1063216146
[5] => 1063255483
[6] => 1063229219
[7] => 1063229480
[8] => 1063216146
[9] => 1063216146
[10] => 1063216146
)
Array
(
)
Array
(
[0] => 1063034516
)
Array
(
[0] => 1063244860
[1] => 1063223237
[2] => 1063216146
[3] => 1063216146
[4] => 1063216146
[5] => 1063226279
[6] => 1063216146
[7] => 1063216146
[8] => 1063228999
)
Array
(
[0] => 1063034516
[1] => 1061227588
[2] => 1063287856
)
Array
(
)
Array
(
[0] => 1062987010
[1] => 1063161402
[2] => 1062986301
)
Array
(
[0] => 1063251980
[1] => 1063034517
)
Array
(
[0] => 1063252588
[1] => 1063211239
[2] => 1062954966
[3] => 1063036204
[4] => 1062954966
[5] => 1063037138
[6] => 1063234393
)
ETC ETC
sorry...the printline should off course be:
print $file_mod_time[sizeof($fil e_mod_time )-1]." ".$win_file_dir[count($win _file_dir) -1] ."<BR>\n";
but this probably doesn't solve your problem...
instead of array_push you can use $array[] =
So the following two lines should give the same result:
array_push($win_file_dir, $tmp[$i]);
$win_file_dir[] = $tmp[$i]
I have no PHP access here. I will look into this some more later. Please pass more results when they become available.
print $file_mod_time[sizeof($fil
but this probably doesn't solve your problem...
instead of array_push you can use $array[] =
So the following two lines should give the same result:
array_push($win_file_dir, $tmp[$i]);
$win_file_dir[] = $tmp[$i]
I have no PHP access here. I will look into this some more later. Please pass more results when they become available.
ASKER
Size of each array I use sizeof outside the while loop.
ct_file_mod_time 1
ct_file_mod_time 55
ct_file_mod_time 5
ct_file_mod_time 11
ct_file_mod_time 0
ct_file_mod_time 1
ct_file_mod_time 9
ct_file_mod_time 3
ct_file_mod_time 0
ct_file_mod_time 3
ct_file_mod_time 2
ct_file_mod_time 7
ct_file_mod_time 11
ct_file_mod_time 1
ct_file_mod_time 1
ct_file_mod_time 14
ct_file_mod_time 3
ct_file_mod_time 59
Thats works fine printout is correct and $win_file_dir[] = $tmp[$i] works the same as array_push.
What I really need to do is have a counter inside the if statement that can grab the size of the previous array
and start the counter at that value each time instead of it being reset to 0
the counter would start at these values ecah time instead of 0. Not sure wheter this is possible or not. Or wheter there is another approach.
$counter would
1
56
61
72
72
73
82
if ((preg_match("/.php/",$dir _files,$ar ray) ))
{
$i++;
$recurse_dir[$i] = $dir_files;
$full_path[$i] = "$path".'/'."$recurse_dir[ $i]";
$tmp[$i] = str_replace('//','/',$full _path[$i]) ;
array_push($win_file_dir, $tmp[$i]);
array_push($file_mod_time, filemtime($tmp[$i]));
}
An alternative approach I used was to write the values to a csv file and then extract them into an array.
I had some odd looking inserts doing this with possible some hidden characters effecting the insert.
It seems a bit long winded anyway.
My aim is to have an array from 0-187. I want to create a seesion variable from this which I will use to insert into a database.
I could use 17 different session vars 1 fro each directory but this seems a bit long winded as well.
ct_file_mod_time 1
ct_file_mod_time 55
ct_file_mod_time 5
ct_file_mod_time 11
ct_file_mod_time 0
ct_file_mod_time 1
ct_file_mod_time 9
ct_file_mod_time 3
ct_file_mod_time 0
ct_file_mod_time 3
ct_file_mod_time 2
ct_file_mod_time 7
ct_file_mod_time 11
ct_file_mod_time 1
ct_file_mod_time 1
ct_file_mod_time 14
ct_file_mod_time 3
ct_file_mod_time 59
Thats works fine printout is correct and $win_file_dir[] = $tmp[$i] works the same as array_push.
What I really need to do is have a counter inside the if statement that can grab the size of the previous array
and start the counter at that value each time instead of it being reset to 0
the counter would start at these values ecah time instead of 0. Not sure wheter this is possible or not. Or wheter there is another approach.
$counter would
1
56
61
72
72
73
82
if ((preg_match("/.php/",$dir
{
$i++;
$recurse_dir[$i] = $dir_files;
$full_path[$i] = "$path".'/'."$recurse_dir[
$tmp[$i] = str_replace('//','/',$full
array_push($win_file_dir, $tmp[$i]);
array_push($file_mod_time,
}
An alternative approach I used was to write the values to a csv file and then extract them into an array.
I had some odd looking inserts doing this with possible some hidden characters effecting the insert.
It seems a bit long winded anyway.
My aim is to have an array from 0-187. I want to create a seesion variable from this which I will use to insert into a database.
I could use 17 different session vars 1 fro each directory but this seems a bit long winded as well.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This might be a daft solution, but ...
<?php
$win_file_dir = array();
$file_mod_time = array();
function util_file_dir_stats ($path)
{
global $win_file_dir,$file_mod_ti me;
...
}
?>
Make the arrays you are adding to global so as you recurse through the directory structure, you are always using an array OUTSIDE of the recursion to store the data.
The following is an old bit of code I use to find the usage of files in directory structures on shared servers (our own servers not ISP ones).
It is a command line version which produces HTML output. The program can take a LONG time to run (try it on your own pc).
<?php
function TreeWalk($sDir,$iCurrentLe vel)
{
global $iLevels;
$aStructure = array(
'Directory' => '',
'Local Files' => 0,
'Local Dirs' => 0,
'Local Size' => 0,
'Total Files' => 0,
'Total Dirs' => 0,
'Total Size' => 0,
'Subs'=>array()
);
$iCurrentLevel++;
$iLevels = ($iCurrentLevel > $iLevels)?$iCurrentLevel:$ iLevels;
$hDir = opendir($sDir);
$aStructure['Directory'] = $sDir;
while (false !== ($sDirEntry = readdir($hDir)))
{
if (($sDirEntry != '.') && ($sDirEntry != '..'))
{
if (is_dir($sDir . '\\' . $sDirEntry))
{
$aStructure['Local Dirs']++;
$aStructure['Total Dirs']++;
$aStructure['Subs'][$sDir . '\\' . $sDirEntry] = TreeWalk($sDir . '\\' . $sDirEntry,$iCurrentLevel) ;
$aStructure['Total Files'] += $aStructure['Subs'][$sDir . '\\' . $sDirEntry]['Total Files'];
$aStructure['Total Dirs'] += $aStructure['Subs'][$sDir . '\\' . $sDirEntry]['Total Dirs'];
$aStructure['Total Size'] += $aStructure['Subs'][$sDir . '\\' . $sDirEntry]['Total Size'];
}
else
{
$aStructure['Local Files']++;
$aStructure['Total Files']++;
$aStructure['Local Size'] += filesize($sDir . '\\' . $sDirEntry);
$aStructure['Total Size'] += filesize($sDir . '\\' . $sDirEntry);
}
}
}
closedir($hDir);
return $aStructure;
}
function TreePrint($aDir,$iCurrentL evel)
{
global $sStartDir,$aStartDir,$iLe vels;
$sReturn = '';
$pF = round(($aDir['Local Files'] * 100) / $aStartDir[0]['Total Files'],2);
$pD = round(($aDir['Local Dirs'] * 100) / $aStartDir[0]['Total Dirs'],2);
$pS = round(($aDir['Local Size'] * 100) / $aStartDir[0]['Total Size'],2);
$pTF = round(($aDir['Total Files'] * 100) / $aStartDir[0]['Total Files'],2);
$pTD = round(($aDir['Total Dirs'] * 100) / $aStartDir[0]['Total Dirs'],2);
$pTS = round(($aDir['Total Size'] * 100) / $aStartDir[0]['Total Size'],2);
$aDir['Local Files'] = number_format($aDir['Local Files'],0,'.',',');
$aDir['Local Size'] = number_format($aDir['Local Size'],0,'.',',');
$aDir['Total Files'] = number_format($aDir['Total Files'],0,'.',',');
$aDir['Total Size'] = number_format($aDir['Total Size'],0,'.',',');
if ($iCurrentLevel > 0)
{
$sPre = <<< END_HTML
<td colspan="$iCurrentLevel">< /td>
END_HTML;
}
else
{
$sPre = '';
}
if ($iCurrentLevel < $iLevels)
{
$iPad = $iLevels - $iCurrentLevel;
}
$sReturn .= <<< END_HTML
<tr>
$sPre
<td colspan="$iPad">{$aDir['Di rectory']} </td>
<td align="right">{$aDir['Loca l Files']}</td><td align="right">$pF%</td>
<td align="right">{$aDir['Loca l Dirs']}</td><td align="right">$pD%</td>
<td align="right">{$aDir['Loca l Size']}</td><td align="right">$pS%</td>
<td align="right">{$aDir['Tota l Files']}</td><td align="right">$pTF%</td>
<td align="right">{$aDir['Tota l Dirs']}</td><td align="right">$pTD%</td>
<td align="right">{$aDir['Tota l Size']}</td><td align="right">$pTS%</td>
</tr>
END_HTML;
$iCurrentLevel++;
if (count($aDir['Subs'] > 0))
{
foreach($aDir['Subs'] as $k)
{
$sReturn .= TreePrint($k,$iCurrentLeve l);
}
}
return $sReturn;
}
$iLevels = 0;
$sStartDir = getcwd();
if (isset($argv) && isset($argv[1]) && is_dir($argv[1]))
{
$sStartDir = $argv[1];
}
$aStartDir[$sStartDir] = TreeWalk($sStartDir,$iLeve ls);
sort($aStartDir);
$sOutput = <<< END_HTML
<table border="1">
<caption>TREE (V1.0.PHP) : (C) UK 2003 Richard Quadling</caption>
<tr>
<th colspan="$iLevels">Directo ry</th>
<th colspan="2">Local files</th>
<th colspan="2">Local dirs</th>
<th colspan="2">Local file size</th>
<th colspan="2">Total files</th>
<th colspan="2">Total dirs</th>
<th colspan="2">Total file size</th>
</tr>
END_HTML;
$sOutput .= TreePrint($aStartDir[0],0) ."</table>\n";
echo $sOutput;
?>
Usage:
tree [<directory>]
(Assuming you can run php scripts from the command line directly and not by having to put c:\php\cli\php.exe at the front.)
To change this to a web based version, you would need to change the $argv into appropiate $_GET/$_POST.
Hope this helps as an example.
Richard.
<?php
$win_file_dir = array();
$file_mod_time = array();
function util_file_dir_stats ($path)
{
global $win_file_dir,$file_mod_ti
...
}
?>
Make the arrays you are adding to global so as you recurse through the directory structure, you are always using an array OUTSIDE of the recursion to store the data.
The following is an old bit of code I use to find the usage of files in directory structures on shared servers (our own servers not ISP ones).
It is a command line version which produces HTML output. The program can take a LONG time to run (try it on your own pc).
<?php
function TreeWalk($sDir,$iCurrentLe
{
global $iLevels;
$aStructure = array(
'Directory' => '',
'Local Files' => 0,
'Local Dirs' => 0,
'Local Size' => 0,
'Total Files' => 0,
'Total Dirs' => 0,
'Total Size' => 0,
'Subs'=>array()
);
$iCurrentLevel++;
$iLevels = ($iCurrentLevel > $iLevels)?$iCurrentLevel:$
$hDir = opendir($sDir);
$aStructure['Directory'] = $sDir;
while (false !== ($sDirEntry = readdir($hDir)))
{
if (($sDirEntry != '.') && ($sDirEntry != '..'))
{
if (is_dir($sDir . '\\' . $sDirEntry))
{
$aStructure['Local Dirs']++;
$aStructure['Total Dirs']++;
$aStructure['Subs'][$sDir . '\\' . $sDirEntry] = TreeWalk($sDir . '\\' . $sDirEntry,$iCurrentLevel)
$aStructure['Total Files'] += $aStructure['Subs'][$sDir . '\\' . $sDirEntry]['Total Files'];
$aStructure['Total Dirs'] += $aStructure['Subs'][$sDir . '\\' . $sDirEntry]['Total Dirs'];
$aStructure['Total Size'] += $aStructure['Subs'][$sDir . '\\' . $sDirEntry]['Total Size'];
}
else
{
$aStructure['Local Files']++;
$aStructure['Total Files']++;
$aStructure['Local Size'] += filesize($sDir . '\\' . $sDirEntry);
$aStructure['Total Size'] += filesize($sDir . '\\' . $sDirEntry);
}
}
}
closedir($hDir);
return $aStructure;
}
function TreePrint($aDir,$iCurrentL
{
global $sStartDir,$aStartDir,$iLe
$sReturn = '';
$pF = round(($aDir['Local Files'] * 100) / $aStartDir[0]['Total Files'],2);
$pD = round(($aDir['Local Dirs'] * 100) / $aStartDir[0]['Total Dirs'],2);
$pS = round(($aDir['Local Size'] * 100) / $aStartDir[0]['Total Size'],2);
$pTF = round(($aDir['Total Files'] * 100) / $aStartDir[0]['Total Files'],2);
$pTD = round(($aDir['Total Dirs'] * 100) / $aStartDir[0]['Total Dirs'],2);
$pTS = round(($aDir['Total Size'] * 100) / $aStartDir[0]['Total Size'],2);
$aDir['Local Files'] = number_format($aDir['Local
$aDir['Local Size'] = number_format($aDir['Local
$aDir['Total Files'] = number_format($aDir['Total
$aDir['Total Size'] = number_format($aDir['Total
if ($iCurrentLevel > 0)
{
$sPre = <<< END_HTML
<td colspan="$iCurrentLevel"><
END_HTML;
}
else
{
$sPre = '';
}
if ($iCurrentLevel < $iLevels)
{
$iPad = $iLevels - $iCurrentLevel;
}
$sReturn .= <<< END_HTML
<tr>
$sPre
<td colspan="$iPad">{$aDir['Di
<td align="right">{$aDir['Loca
<td align="right">{$aDir['Loca
<td align="right">{$aDir['Loca
<td align="right">{$aDir['Tota
<td align="right">{$aDir['Tota
<td align="right">{$aDir['Tota
</tr>
END_HTML;
$iCurrentLevel++;
if (count($aDir['Subs'] > 0))
{
foreach($aDir['Subs'] as $k)
{
$sReturn .= TreePrint($k,$iCurrentLeve
}
}
return $sReturn;
}
$iLevels = 0;
$sStartDir = getcwd();
if (isset($argv) && isset($argv[1]) && is_dir($argv[1]))
{
$sStartDir = $argv[1];
}
$aStartDir[$sStartDir] = TreeWalk($sStartDir,$iLeve
sort($aStartDir);
$sOutput = <<< END_HTML
<table border="1">
<caption>TREE (V1.0.PHP) : (C) UK 2003 Richard Quadling</caption>
<tr>
<th colspan="$iLevels">Directo
<th colspan="2">Local files</th>
<th colspan="2">Local dirs</th>
<th colspan="2">Local file size</th>
<th colspan="2">Total files</th>
<th colspan="2">Total dirs</th>
<th colspan="2">Total file size</th>
</tr>
END_HTML;
$sOutput .= TreePrint($aStartDir[0],0)
echo $sOutput;
?>
Usage:
tree [<directory>]
(Assuming you can run php scripts from the command line directly and not by having to put c:\php\cli\php.exe at the front.)
To change this to a web based version, you would need to change the $argv into appropiate $_GET/$_POST.
Hope this helps as an example.
Richard.
Yes.
function util_file_dir_stats ($path)
{
if ($dir=@opendir($path))
{//OUTER IF
while (($dir_files=readdir($dir)
{//START WHILE
if (is_dir($path.$dir_files) AND $dir_files!= "." AND $dir_files!= "..")
{
//Places all Directory names in $recurse_dir
if ($dir_files == strtolower($dir_files))
{
$i++;
$recurse_dir[$i] = util_file_dir_stats($path.
}
}
elseif($dir_files!= "." AND $dir_files!= "..")
{//IF 1
if ((preg_match("/.php/",$dir
{
$i++;
$recurse_dir[$i] = $dir_files;
$tmp = str_replace('//','/',$path
array_push($win_file_dir, str_replace('//','/',$tmp)
array_push($file_mod_time,
print "$file_mod_time[count($fil
}//END IF 1
}//END WHILE
closedir($dir);
}//END OUTER IF
return($array);
}