I'm attempting to build a website on my computer and I'm having issues linking with my root directory from anywhere on the website. I know I can use ../
to go up 1 directory as many times as needed, but I need a way to link to my root directory without using the whole address so I can use "absolute links" to use in my PHP pages which I want to use all over my site for ease of editing. I've been told that /
will take me back to the root, but it isn't working on my computer.
Here is the address for my deepest file (in my 3rd directory from the root) http://localhost/_Website%20Files/cats/articles/_Blank-Cats2.php
The file I want is in _Website Files in the middle.But that's a folder INSIDE localhost, which I believe is being used as the root. There is a file within Localhost called test.php
that I tried to link to to test whether LocalHost was being used as the root, but it didn't show up in _Blank-Cats2.php where I tried it. (does that make sense?) Somewhere in _Blank-Cats2.php there should have been a sentence that said "Php test for website", and it didn't show up.
Also, I'm using MAMP as my php localhost. If that helps anyone in anyway.
Am I doing something wrong, or is the problem with trying to link to a root directory on my computer?
------Edit------ I've changed my root directory to _Website Files, and now some of my /links are working, but others aren't.
For example, in my Social-Media-Menu.php
file which I've included in my _Blank-Cats2.php
page, using /images/facebook.png
worked to locate the image. However, I cannot use /Social-Media-Menu.php
to locate and include the php file, I have to use ../../Social-Media-Menu.php
or it won't work. Also, I was able to link to my style page with /styles.css
from the save _Blank-Cats2.php
page, but it wont work with my php pages. I was also able to make /images/logo.gif
work.
So basically I can use /
to link to my /images
directory, but not anything IN the root directory itself. For that I need to use ../
or ../../
Maybe I'll just create a new directory just for php includes and see if that solves anything.
------Edit------- So I created a new directory called PHP-includes
and I put all the files I want included elsewhere in there. header.php
, menu.php
, etc. But when I went to use /PHP-includes/header.php
, it didn't work.
However I HAVE noticed that the elements that ARE working are html/css
based. My /styles.css
is working, my /images/logo.gif
is working. The only things NOT working are things I'm trying to include with PHP, so I think it has something to do with how PHP works. Could that be the case?
-----Edit------- Aha! /
will NOT work with PHP, I found that out here https://css-tricks.com/php-include-from-root/ So I guess I will need to mess around with my PHP code in order for it to behave and do what I want it to do. If you have any tips or tricks on how to do that (the example listed on that page I posted didn't makes sense to me) please feel free to post below =)
The relative paths will not work well with your PHP files. The solution I gave you here...
How do I link to a file in a different directory without using the whole address?
... was meant to work for client side resources only (HTML/CSS/Images). If you want to get the root directory in your PHP code you have to use $_SERVER["DOCUMENT_ROOT"]
:
Then we would use something like this for all our includes:
<?php include($_SERVER["DOCUMENT_ROOT"] . "/your/path.php"); ?>
You have to understand that some resources are loaded in server side (PHP Server) and what is loaded on client side (browser). That's why you can't do a generic solution working for both sides.