chrismassey
Programmer
Hello,
I have a function which I use for displaying the entire contents of a directory (a recursive process). It works great, but the only problem is I also use it for a search process (to search for a particular file) and at the end of the search I want to display how many directories were found and how many files were found. I basically do this by adding 1 to $dir_count for a directory and $file_count for a file. I then return these results so that they can be displayed at the end.
However, I am not totally sure why, but I think because of the recursive nature of the function, the result is not returned once at the very end, but returned many times (for example if there are 3 levels of directories, then the result will be returned 3 times) and inevitably the result displayed is the result of the final return.
Does anyone have any suggestions how I can improve my code so that it returns an accurate result of how many directories/files have been found?
Thank you very much,
Chris
(I cleaned the code up so only code regarding search remains):
I have a function which I use for displaying the entire contents of a directory (a recursive process). It works great, but the only problem is I also use it for a search process (to search for a particular file) and at the end of the search I want to display how many directories were found and how many files were found. I basically do this by adding 1 to $dir_count for a directory and $file_count for a file. I then return these results so that they can be displayed at the end.
However, I am not totally sure why, but I think because of the recursive nature of the function, the result is not returned once at the very end, but returned many times (for example if there are 3 levels of directories, then the result will be returned 3 times) and inevitably the result displayed is the result of the final return.
Does anyone have any suggestions how I can improve my code so that it returns an accurate result of how many directories/files have been found?
Thank you very much,
Chris
(I cleaned the code up so only code regarding search remains):
Code:
function overview($main_action, $script_url, $overview_path, $ii_url, $p_tb1, $p_sb2, $dir_count, $file_count, $i_gap_count, $file_carry, $cm_carry, $cm_action, $dir_path) {
$path = $overview_path.'/'.$dir_path;
$url = $ii_url.'/'.$dir_path;
$handle = opendir($path);
while($file = readdir($handle)) {
if ($file == '.' || $file == '..') {
continue;
}
$new_dir_path = $dir_path.'/'.$file;
$new_path = $path.'/'.$file;
$new_url = $url.'/'.$file;
if(is_dir($new_path)) {
if (stristr($file, $p_tb1) === FALSE) { }
else {
echo "<img src=\"Images/Directory.bmp\" title=\"Directory\"> <a href=\"$script_url?path_carry=c&file_carry=$file_carry&cm_carry=$cm_carry&cm_action=$cm_action\" title=\"$new_dir_path\"><b>$file</b></a><br>";
$dir_count++;
}
overview($main_action, $script_url, $overview_path, $ii_url, $p_tb1, $p_sb2, $dir_count, $file_count, $i_gap_count, $file_carry, $cm_carry, $cm_action, $new_dir_path);
}
else {
if (stristr($file, $p_tb1) === FALSE) { }
else {
echo "<img src=\"Images/File.bmp\" title=\"File\"> <a href=\"$new_url\" target=\"_blank\" title=\"$new_dir_path\">$file</a><br>";
$file_count++;
}
}
}
closedir($handle);
$return = "$dir_count|$file_count";
return $return;
}