This is what you need:
dirname($path)
dirname: Given a string containing the path of a file or directory, this function will return the parent directory's path.
__FILE__: The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.
--
Inside template.php, __FILE__ is the absolute path to template.php:
/[path to your app]/template.php
Then dirname(__FILE__) return:
/[path to your app]/
--
Inside index.php, __FILE__ is the absolute path to index.php:
/[path to your app]/pages/requiredfoldername/index.php
Then dirname(__FILE__) return:
/[path to your app]/pages/requiredfoldername
Also in template.php, maybe $_SERVER would help you with the key 'SCRIPT_FILENAME'. This key contains the absolute path (with exceptions in CLI) to the currently executing script:
If /pages/requiredfolder/index.php is your entry point to the app, when you include template.php, the currently executing script remains index.php. So $_SERVER['SCRIPT_FILENAME'] will be
'/[path to your app]/pages/requiredfoldername/index.php'
Using dirname with $_SERVER['SCRIPT_FILENAME'] results in:
'/[path to your app]/pages/requiredfoldername'
getcwd()? php.net/getcwd