Im new to php but im getting there slowly. One major problem I have at the moment is with php includes.

Code:
<?php if (isset($_GET['x'])) {
   if (strpos($_GET['x'], "/")) {
      $dir = substr(str_replace('..', '', $_GET['x']), 0, strpos($_GET['x'], "/")) . "/";
      $file = substr(strrchr($_GET['x'], "/"), 1);
      if (file_exists($dir.$file.".php")) {
         include($dir.$file.".php");
      } else {
         include("pagefiles/error.php");
      }
   } else {
      if (file_exists(basename($_GET['x']).".php")) {
         include(basename($_GET['x']).".php");
      } else {
         include("pagefiles/error.php");
      }
   }
} else {
   include("pagefiles/error.php");
} ?>
Is the code im using, however it only seems to let me use one directory. I have a fairly large site now and some of my folders are 4 deep.

How could I change it so that links such as:-

index.php?x=pagefiles/portfolio/sites/1

would work?

Thanks