Issues with relative/absolute path

90 views Asked by At

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.

3

There are 3 answers

1
Pratik Joshi On BEST ANSWER

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.

0
dors On

This is most likely one of your other scripts includes the config.php without proper path. Set up your include paths using the following code

  set_include_path(get_include_path() . PATH_SEPARATOR . $root_path);

Also change the include to require_once to prevent multiple includes.

0
Rash On

Your root path does not points to the actual root of your project. Your actual root is someLocation/yourProject.

the root you have defined is someLocation/yourProject/includes/

Then you want to include file in another folder. Hence it cannot find it. Define the root of your path to your actual project root and not inside includes directory.

To do this, you can define the root path in your config file and read it from there.