I am stumped by a 'cant redeclare function' error.. please help!

no, i am not declaring it twice (afaik), yes, i am using include_once but i just cant resolve it.

the error is this:

Fatal error: Cannot redeclare getdirlist() (previously declared in /home/xplouk/public_html/counterspace/logic.php:10) in /home/xplouk/public_html/counterspace/logic.php on line 10

i have this code at the top of my index page:

<?php
	session_start();
	include_once ("logic.php");	

if(!session_is_registered ('coords'))
{
	session_register ('coords');
	session_register ('projectsno');
	session_register ('arrow');
	session_register ('projects');
	$HTTP_SESSION_VARS ['coords'] = $coords;
	$HTTP_SESSION_VARS ['projectsno'] = $projectsno;
	$HTTP_SESSION_VARS ['arrow'] = $arrow;
	$HTTP_SESSION_VARS ['projects'] = $projects;
	$coords = create_coords();
	$projectsno = countdirs('img');
	$projects = listdirs('img');
	$arrow = get_arrow($projectsno);
} 		

?>

this is the function in the logic.php page - its recursive and is used to get the number of directories on the server:

	function countdirs($base_dir)
	{
			global $dirs;
			   function getDirList($base)
			   {
			   global $dirs;
			   if(is_dir($base))
				   {
					   $dh = opendir($base);
					   while (false !== ($dir = readdir($dh)))
					   {
					   if (is_dir($base ."/". $dir) && $dir !== '.' && $dir !== '..') 
						   {
							   $subbase = $base ."/". $dir;
							   $dirs[]=$subbase;
							   getDirList($subbase);
						   }
					   }
					   closedir($dh);
				   }
			   }

		getDirList($base_dir);
		$retval = count($dirs);
		return $retval;
}

it is clearly pointing to the same function but i cant see what i have done wrong!! 😕

help really appreciated! 🙂

    try moving getdirlist() outside the countdirs() function.

    function getDirList($base)
      {
      global $dirs;
      if(is_dir($base))
        {
          $dh = opendir($base);
          while (false !== ($dir = readdir($dh)))
            {
              if (is_dir($base ."/". $dir) && $dir !== '.' && $dir !== '..')
                {
                  $subbase = $base ."/". $dir;
                  $dirs[]=$subbase;
                  getDirList($subbase);
                }
            }
          closedir($dh);
        }
      }
    
    function countdirs($base_dir)
      {
         global $dirs;
         getDirList($base_dir);
         $retval = count($dirs);
         return $retval;
      }

      ...Otherwise it will try to redeclare getDirList() every time countdirs() is called.

        i did realise after much headscratching what was going on.. but thanks for the advice guys 😃

          Write a Reply...