0

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.

15
  • Can you please, describe the path to css and js file as relative to php index.php, that where you want to keep them and where index.php is. Commented Oct 20, 2017 at 23:52
  • Also please add a bit of code snippet from where you are using this. Personally, I've never used DOCUMENT_ROOT superglobal for referencing where all my js and css files are located for html output to the browser. So I'm curious how you are using it. Commented Oct 20, 2017 at 23:54
  • Differentiate between the server-side file system, and the website URL structure. The latter can overlap with the former, but $_SERVER ["DOCUMENT_ROOT"] will hardly ever have anything to do with client-side URLs. Commented Oct 21, 2017 at 0:03
  • @CBroe my css folder & JS folder reside in the root of my public_html directory. So file structure is as so: public_html/css; public_html/JS folders, then I have folders: public_html/folder1/index.php; public_html/folder2/index.php and so on. But PHP doesn't see it this way as my includes folder that holds my header, sidebar and footer php files also reside in the root of public_html. PHP is telling me that my css and js files should be INSIDE the same directory where my index.php file resides Commented Oct 21, 2017 at 2:01
  • ($_SERVER ["DOCUMENT_ROOT"] . "/includes/header.php"); Commented Oct 21, 2017 at 2:02

1 Answer 1

1

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";
Sign up to request clarification or add additional context in comments.

3 Comments

dir(__DIR__) not just __DIR__ ??
So making a variable and creating the value to the directories in this way, becomes an absolute path instead of a relative path.
@mega6382 this didn't seem to work for me... my file structure in xammp is as such - htdocs/myprojectnet (root)/css, js, and includes foders. Then my directories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.