0

Before I start, this question may have been asked before, but I either don't know what to type to find it specifically and this is a slightly more specific case.

I'm developing a website with many dynamic ties, some of which is at the beginning of every php file there is the line require("global.php"); which does what the name states, among others such as the css file and whatever else is found on the root level. Problem is however, upon entering down into another directory, the link to this file is broken.

Traditionally what I've done (which is an absolutely stupid beginner workaround) is create a variable just before the require called $up which contained the string '../' which recurred respectively depending on how many directories deep the file is from the root. So the line would then appear require($up."global.php");.

I've realised how stupid this is and have tried to find ways around this. One of which is using the variable $_SERVER['DOCUMENT_ROOT'] to print out the root directory but the reason why this won't work 100% is because a) I'm developing the website on a local machine where the 'root' of the website in development is located in a subdirectory on the server and b) it returns the entire path based upon the location on the drive starting from D:\ and working its location that way, which I don't particularly like to work with when it comes to developing websites and would rather remain within the hosted directories of the web server if possible.

So, based on what I've explained, is there an easy way to get the root location of a website (regardless if the project is in a subridectory or the real root of the web server) within a short string that is extremely easy to append to the begining of every file reference in every php file regardless of it's directory level?

Thanks! <3

SteppingHat
  • 1,199
  • 4
  • 19
  • 50
  • http://stackoverflow.com/questions/2311983/how-can-i-get-the-application-root-of-my-url-from-php – sridesmet Nov 05 '14 at 13:52
  • @Gudgip Doing what the accepted answer gives me is a directory from the root of the drive. Being on a mac this is `/Applications/XAMPP/xamppfiles/htdocs/website3`. Is there any way to strip it down to start from `website3` without the use of any other code to modify the string? Also, how would I then store that as a constant? Constants are new to me and I don't fully understand how they work – SteppingHat Nov 05 '14 at 14:20
  • Hmm, one way to do this is to save the directory where your index.php file resides in a constant (or output it to a file or whatever), and then use that in all your other files, so in your other files: `require ($MY_ROOT.'/relative/path/to/include/file.php')` This way it looks relative, but it's actually absolute. – sridesmet Nov 05 '14 at 14:32
  • http://php.net/manual/en/language.constants.php – sridesmet Nov 05 '14 at 14:35
  • As much as that would work, the reason for it being in a subdirectory to begin with is purely for local development. When it becomes time to move it to the main web server, it will then be located in the real root of the webserver which then breaks this approach. – SteppingHat Nov 05 '14 at 14:53

1 Answers1

0

Suppose you have the following directory:

 /srv/website/index.php
 /srv/website/lib/functions.php

You could then do this:

define("MY_ROOT",     $_SERVER['DOCUMENT_ROOT']);

And use it in every (included) file relatively:

require (MY_ROOT . "lib/functions.php" );
sridesmet
  • 875
  • 9
  • 19
  • It almost works, however it does not account for that the so called 'root' is actually a subdirectory within the real root. Is there a way to make this work so that when transferring it to the hosted web sever where the website root then becomes the real root that it still works without breaking any links? – SteppingHat Nov 05 '14 at 14:50
  • You could adjust the constant manually. (adding `..\ `) You'll only have to edit the `define`, or do you want an automatic solution? Because that'll be harder imo. – sridesmet Nov 05 '14 at 15:00
  • Wouldn't that mean I'll have to edit the `define` in every single file? It would defeat the purpose of replacing the idea of having an `$up` variable at the beginning of every file in one sense – SteppingHat Nov 05 '14 at 15:30
  • No. http://stackoverflow.com/questions/10691404/php-best-way-to-define-global-constant-available-in-all-files – sridesmet Nov 07 '14 at 07:34