Forum Moderators: coopster

Message Too Old, No Replies

I can't fathom this out!

loop within a loop!

         

Matthew1980

8:36 am on Dec 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello people of WebmasterWorld!

I have this function:-

function ScanDirFiles(Array $FileType){

if($this->populated){
//check that the users supplied dirs are set & have value

if(isset($this->UserDirs) && !empty($this->UserDirs)){
//Foreach dir that is scanned, we need to echo the list of dirs/filenames

//as they appear in the dir tree in explorer:-
/*
folder1
|
---->File1
|
---->File2
|
---->File3
folder2
|
---->File1
|
---->File2
|
---->File3
*/

//only put this here just in case I need to have a count done of total folders
//supplied as a variable (currectly stands at 3)
$CountedDirs = count($this->UserDirs);

//This is the part I can't fathom out:-

//Loop for directory names works fine
foreach($this->UserDirs AS $Key=>$FolderName){

echo $FolderName."<br>";
//loop through dirs and print values to screen that aren't system . or ..
while($GotFiles = readdir(opendir($FolderName))){

if($GotFiles != "." && $GotFiles != ".."){
echo $GotFiles."<br>";
}
}
}
exit;
//terminated script here intentionally

Within the foreach, I want to have another loop which reads the file contents of the folders that have been given up to this point.

I get the folder list, but the only thing I get when I try to run a loop within a loop, is the 60s timeout warning

As it stands there ^^ I have the directory listing working fine, but when I try to pass those values into another loop so that I can process the files that are listed within each given directory, this is where it falls apart.

I just cannot fathom this out - any help or pointers are greatly appreciated.

Cheers,
MRb

Matthew1980

10:27 am on Dec 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



UPDATE:-

I have figured it out I think!

This line (in bold):-

foreach($this->UserDirs AS $Key=>$FolderName){
echo $FolderName."<br>";
$FilesHandle = opendir($FolderName);<-- this needs to be outside the readdir() NOT inside it

while (false !== ($GotFiles = readdir($FilesHandle))) {
if(($GotFiles != ".") && ($GotFiles != "..")){
echo $GotFiles."<br>";
}
}
}

Now as I have changed this, I get the result I wanted, I hadn't realised that you can't nest those functions..

Cheers,
MRb