Page 1 of 1

problem with a function

Posted: Thu Jun 03, 2004 6:36 pm
by dull1554

Code: Select all

<?php
$base = "c:/folder/";
function getDirList($base)
{
   $int = 0;
   if(is_dir($base)){
       $dh = opendir($base);
       while (false !== ($dir = readdir($dh))) {
           if (is_dir($base . $dir) && $dir !== '.' && $dir !== '..') {
               $subs = $dir;
               $subbase = $base . $dir . '/';
               print $subbase . "
";
               $array[$int] = $subbase;
               $int++;
               getDirList($subbase);
           } 
       }
       closedir($dh);
   }
   return $array;
}
getDirList($base);
print_r($array);
?>
this is a function slighlty modified from the php manual usernotes, it prints out the directories but if i try to assign the values to an array and then return the array, it telles me that it does not exist
even though when i run it it prints out 4 dirs

Code: Select all

c:/folder/adf/
c:/folder/adf/New Folder/
c:/folder/adf/New Folder/asdfa/
c:/folder/asdasdfsad/
and these valuse should also be assigned to the array but they arent
any thoughts?

p.s. if i get this to work it will be a soultion to an earilier question/ or a means to an end rather

Posted: Thu Jun 03, 2004 6:41 pm
by markl999
getDirList($base); should be $array = getDirList($base); otherwise the returned $array 'gets lost'.
I'd also initialise $array in the function...
$int = 0;
$array = array();

Posted: Thu Jun 03, 2004 6:47 pm
by dull1554
now when i run it i get this error

Notice: C:\Program Files\Apache Group\Apache\htdocs\indexbuilder.php line 25 - Undefined variable: array

new code:

Code: Select all

<?php
$base = "c:/folder/";
function getDirList($base)
{
   $int = 0;
   $array = array();
   if(is_dir($base)){
       $dh = opendir($base);
       while (false !== ($dir = readdir($dh))) {
           if (is_dir($base . $dir) && $dir !== '.' && $dir !== '..') {
               $subs = $dir;
               $subbase = $base . $dir . '/';
               print $subbase . "
";
               $array[$int] = $subbase;
               $int++;
               $array = getDirList($subbase);
           } 
       }
       closedir($dh);
   }
   return $array;
}
getDirList($base);
print_r($array);
?>
any thoughts?

Posted: Thu Jun 03, 2004 6:48 pm
by markl999
getDirList($base); should be $array = getDirList($base); otherwise the returned $array 'gets lost'.
Sorry i meant to do that when you call the function outside the function, not where you iteratively call it inside the function.

Code: Select all

<?php
$base = "c:/folder/"; 
function getDirList($base)
{
   $int = 0;
   $array = array();
   if(is_dir($base)){
       $dh = opendir($base);
       while (false !== ($dir = readdir($dh))) {
           if (is_dir($base . $dir) && $dir !== '.' && $dir !== '..') {
               $subs = $dir;
               $subbase = $base . $dir . '/';
               print $subbase . "
";
               $array[$int] = $subbase;
               $int++;
               getDirList($subbase);
           }
       }
       closedir($dh);
   }
   return $array;
}
$array = getDirList($base);
print_r($array);
?>

Posted: Thu Jun 03, 2004 7:08 pm
by dull1554
oh...wow...a little dense

Posted: Thu Jun 03, 2004 7:10 pm
by dull1554
now it works but it wont work inside the function

Code: Select all

<?php
$base = "c:/folder/";
function getDirList($base)
{
   $int = 0;
   $array = array();
   if(is_dir($base)){
       $dh = opendir($base);
       while (false !== ($dir = readdir($dh))) {
           if (is_dir($base . $dir) && $dir !== '.' && $dir !== '..') {
               $subs = $dir;
               $subbase = $base . $dir . '/';
               print $subbase . "
";
               $array[$int] = $subbase;
               $int++;
               getDirList($subbase);//this is not working or not running at all
           } 
       }
       closedir($dh);
   }
   return $array;
}
$array = getDirList($base);
print_r($array);
?>
it returns the base directories inside the directory specified

Posted: Thu Jun 03, 2004 7:29 pm
by markl999
Try this version.

Code: Select all

<?php
$base = "c:/folder/";
function getDirList($base, $array = array())
{
   if(is_dir($base)){
       $dh = opendir($base);
       while (false !== ($dir = readdir($dh))) {
           if (is_dir($base . $dir) && $dir !== '.' && $dir !== '..') {
               $subbase = $base . $dir . '/';
               echo $subbase.'<br />';
               $array[] = $subbase;
               $array = getDirList($subbase, $array);
           }
       }
       closedir($dh);
   }
    return $array;
}
$array = getDirList($base);
print_r($array);
?>

Posted: Thu Jun 03, 2004 7:33 pm
by dull1554
that works perfectly
thanks a bunch

Posted: Thu Jun 03, 2004 8:29 pm
by dull1554
ok ive gotten quite a ways now

heres what i have, when i try to use in_array()
it tells me that im using the wrong datatype, but i know im using it right, a string for the needle, and an array for the array

Code: Select all

<?php
$base = "c:/folder/";
function getDirList($base, $array = array())
{
   if(is_dir($base)){
       $dh = opendir($base);
       while (false !== ($dir = readdir($dh))) {
           if (is_dir($base . $dir) && $dir !== '.' && $dir !== '..') {
               $subbase = $base . $dir . '/';
               $array[] = $subbase;
               $array = getDirList($subbase, $array);
           }
       }
       closedir($dh);
   }
   if(!empty($array)){
    return $array;
   } else {
    return array();
   }
}
$array = getDirList($base);
function getDirFiles($dirPath)
{
	if ($handle = opendir($dirPath)) 
    {
    	while (false !== ($file = readdir($handle)))
    	{
    		if ($file != "." && $file != "..") 
    		{
    			$filesArr[] = trim($file);
    		}
    	}
        closedir($handle);
    }  
    return $filesArr; 
}
function parsearray($array)
{
	$num = count($array);
	for($i = 0; $i <= $num; $i++)
	{
        $filearray = getDirFiles($array[$i]);
        print_r($filearray);
        if(in_array("index.php", $filearray))
        {
        }
        else
        {
        	$fp = fopen("index.php", 'a+');
        	$fc = fclose($fp);
        }
	}
	
}
parsearray($array);
?>

Posted: Thu Jun 03, 2004 8:33 pm
by markl999
for($i = 0; $i <= $num; $i++)
should be
for($i = 0; $i <= $num-1; $i++)

(or use a foreach instead)

[edit] or for($i = 0; $i < $num; $i++)

:o

Posted: Thu Jun 03, 2004 8:42 pm
by dull1554
i still get
Warning: in_array(): Wrong datatype for second argument in c:\program files\apache group\apache\htdocs\indexbuilder.php on line 45
and i have never really used a foreach statement, ive been looking at the manual but it does not really explain it too well