PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
This all works fine, however, I'm guessing my design/code is not the best, as I have to clear the array before running the function again. If I don't clear the array, then the next instance of the function simply adds to the previous. So my question is, how can I better code my function to allow it to be used multiple times, but without having to clear it each time? thanks.
Return an array from the function, when you recurse, add the returned data to the current array that will be returned when that function ends. You have to be careful with recursion though, as you can quickly run out of memory because of all the jumps in and out of the function.
I prefer to write the function in a single go. This is done by getting the directory list of an initial directory, then iterating over the results looking into each directory contained within it. If I must, I'll post some code, but I'd like you to try making it (if you want to) first.
As it stands at the moment, if I don't use globals, and leave the code exactly as is except comment out the global $loop_dir line, then I get the error "Undefined variable $loop_dir" when I run my page. I'm not sure I completely understand what you think I should do feyd. I sort of understand what it is you reccomend, but am not sure how to implement it. However I'm not looking for a free ride, so if you can explain it any better (as opposed to posting code) that would be appreciated thanks
okay.. to start, create a function that takes a directory as an argument. Upon calling, store the passed directory make it the initial element in an array. Using a for loop, you can now iterate over that array, doing a check if the current entry is a directory, if so, loop over it. Each entry in that directory found, add to the original array. The limit control of the for loop (second expression) must use an updatable flag of sorts.
Ok well unfortunately I haven't been able to successfully do it using your methodology Feyd. I did up the following code, trying take on board what you suggested:
function new_loop($folder) {
$new_array[] = $folder;
$count = 1;
// how should this work ??
while ($count ??) {
foreach ($new_array as $dir) {
if (is_dir($dir)) {
$dir_array[] = $dir;
echo 'Dir: '.$dir.'<br>';
$handle = opendir($dir);
if ($handle) {
while (false !== ($file = readdir($handle))) {
$new_array[] = $file;
$count++; // ?
}
}
}
}
}
return $dir_array;
}
I return $dir_array because I only want the directories, not individual files. I know i should use my $count variable somehow, but am unsure how to go about it? ie. what I should be comparing it to...I've tried using the size of the $new_array etc, but it seems just to create a never ending loop
the while and foreach can be swapped out for a single for() if done correctly. readdir() only returns the filename, not a full path, so you need to add $dir to it.