Destry
Destry

Reputation: 3

PHP using $_SERVER["DOCUMENT_ROOT"] and relative or absolute paths

I'm using XAMMP and using this variable in my index.php pages:

($_SERVER ["DOCUMENT_ROOT"] . "/path/to/my/php/file);"

My file structure is like so located in the htdocs folder:

myprojectnet/include

   myprojectnet/css

      myprojectnet/js

        myprojectnet/folder1/index.php, css folder and js folder, etc;

            myprojectnet/folder2/index.php css folder and js folder, etc;

Inside my index.php files I've used this variable:

($_SERVER ["DOCUMENT_ROOT"] . "/include/header.php"); 

Inside my header.php file the structure is like so:

<HTML>
<HEAD>

<link rel="stylesheet" type="text/css" href="css/name.css">

<script src="js/jquery.js"></script>

I'm having to put my css folder and js folder inside the same directories where my index.php file resides and I shouldn't have to do this when my header.php file should be resolving the path to my css folder and js folder correctly.

Upvotes: 0

Views: 1394

Answers (1)

mega6382
mega6382

Reputation: 9396

Lets say that all js and css files are in root/assets/css and root/assets/js. And all php files are in like root/main/index.php or root/other/index.php, you can simply use something like this:

$baseDir = dirname(__DIR__);

$cssFile = $baseDir."/assets/css/style.css";

$jsFile = $baseDir."/assets/js/index.js";

Upvotes: 1

Related Questions