Search code examples
phppathrelative-pathrealpathpathinfo

Issues with relative/absolute path


I've searched ways to do it so it would work but none of them seemed to work for all paths, so I was wondering if you could give me a direction on how to do this:

This is the structure:

Home Directoy
   config.php
   index.php
   includes/
      user_login.php
      applicant_track.php
   ucp/
      index.php

When I am trying to include includes/user_login.php in ucp/index.php it doesn't let me and says that it cannot find the file, my code is in ucp/index.php is:

if(!defined('root_path'))
{
    define('root_path', realpath(dirname(__FILE__)));
}

include(root_path . '\config.php');

switch($_GET["a"])
{
    case null: include(root_path . '\index.php'); break;
    case "login": include(root_path . '\includes\user_login.php'); break;
}

This is what I get:

Warning: include(E:\xampp\htdocs\aod_panel\ucp\config.php): failed to open stream:

I'd be happy for an advise on how to fix this.


Solution

  • Use following path

    define('root_path', realpath(dirname(dirname(__FILE__))));
    

    instead of your code for defining real path. As your folder structure is like that.

    See your index.php is in ucp folder but you want path of config.php. So go back one directory and get config.php path.