0

I'm using a complicated folder structure for my website, which makes organizing my content simple. However when I need to link to a page I often have to take this approach:

include_once '../../../assets/php/DB/DBConnect.php';

Is there a way for me to get rid of all the ../ things?

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Giani Noyez
  • 481
  • 3
  • 8
  • 17
  • `$_SERVER["DOCUMENT_ROOT"]` if "assets" is your first sub-folder from root, yes. I.e.: `$_SERVER["DOCUMENT_ROOT"] . "/path/to/file";` – Funk Forty Niner Aug 10 '15 at 11:09
  • @Fred-ii- im bit confuce about duplicated answer that you marked. Cz its feel not relavant to this. Its shows different in `dodument root` and he asking how to access file using provided details. Thank you – Abdulla Nilam Aug 10 '15 at 11:53

2 Answers2

1

Try

Method 01

$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/assets/php/DB/DBConnect.php";
include_once($path);

Method 02

include_once $_SERVER["DOCUMENT_ROOT"].'/assets/php/DB/DBConnect.php';

Note

I have found that in some environments DOCUMENT_ROOT does not seem to be properly set. I have devised a way that is independent of the server, and whether the hosting provider provides the ability to set an include or auto-prepend path as well.

Each directory contains a 'meta' file called '__php__.php' (or whatever one wants to call it). For any files in that directory, they simply include it as <?php include('__php__.php'); ?>. The file itself simply includes the one in the parent directory all the way up to the site root. The file in the site root then can include other files, a simply way of auto-including files even if a service provider does not support it, and also define a variable such as 'SITE_ROOTDIR', which can then be used later. If the document files are moved to another directory, they will still include the __php__.php file in that directory and still get the SITE_ROOTDIR constant from the top __php__.php file.

I also do something similar for a simple navigation bar, where each directory has a __navbar__.php file, and each page simply includes it at the correct location and it can include parent navigation elements and define its own navigation elements.

One advantage of this is that a part of the site can be sectioned in a sub-directory, and still get the root of that part, even if it isn't the actual document root. A disadvantage is that it may slow down while doing all the includes on a site with heavy traffic.

Yet another:

<?php set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] ); ?>
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

Change from

include_once '../../../assets/php/DB/DBConnect.php';

To

include_once $_SERVER["DOCUMENT_ROOT"].'/assets/php/DB/DBConnect.php';

Note assets should be your first sub-folder.

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50