8

To keep URLs working in version-controlled projects, I've been using $_SERVER['DOCUMENT_ROOT']. The problem is, I develop projects within a folder, so I get this:

$_SERVER['DOCUMENT_ROOT'] . '/folder/path/to/file.php'

When I go live, I generally simply want the following:

$_SERVER['DOCUMENT_ROOT'] . '/path/to/file.php'

I know there are bigger problems in the world than having to remove and add this folder name, but is there a way I can easily automate this? Can I somehow set my document root locally to include the folder I'm working in? Do I have a fundamental misunderstanding of the way things are working? Kind of new at this stuff, and looking to learn as much as possible and really grok the "why."

Thanks so much!

Joshua Cody
  • 3,742
  • 5
  • 32
  • 34

2 Answers2

10

Instead of using $_SERVER['DOCUMENT_ROOT'], why not declare a constant which always holds the root of your web application?

<?php
define('ABSPATH', dirname(__FILE__));

Put the following code in a file located in the root folder of your application and include it on every page load.

Then, you can simply always do $path = ABSPATH . '/path/to/file.php'; regardless of if your local copy is in a sub-directory folder or not.


If your application already has a file which is included on every page load, you can simply drop the code above in that file and it will work.

Just note that you may have to add additional dirname() calls depending on where that file is located. Add one for each directory you pass from the root of your webapp.

For example, if your webapp is located in /webapp/ and your "global include" is located in /webapp/includes/framework/init.php, then the above code needs to be modified as such:

define('ABSPATH', dirname(dirname(dirname(__FILE__))));

ie.: 2 additional dirname() calls due to two additional folders from the webapp root (includes/framework)


Clarification

The code above is meant to be in one file, and one file only in your web application. That file needs to be included on each page load.

If you already have a file which is included before any processing (such as a configuration file or other), you may copy and paste that code in that file.

The number of dirname() calls depends on how deep the file you copied and pasted the code in is relative to the root directory of your web application. For the examples above, assume the root of your web application is represented by ~.

If you copy-paste my code into ~/abspath.php, then you need one dirname() call.

If you copy-paste my code into ~/includes/abspath.php, then you need two dirname() calls.

If you copy-paste my code into ~/includes/config/abspath.php, then you need three dirname() calls. Now let's just say that's its final location.

In ~/index.php, you do the following:

<?php
require_once('includes/config/abspath.php');

and you have access to ABSPATH.

In ~/dir/someOtherPage.php you do the following:

<?php
require_once('../includes/config/abspath.php');

and you have access to ABSPATH.

This is why I'm saying that if you already have a file which is included on each page load, its simpler just to drop the above code in it. Just make sure you modify the amount of dirname() calls accordingly. Again, this code is meant to be in ONLY ONE FILE.

Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
  • All right Andrew, your method is a little harder to grasp at first. Let me check I'm getting it: Each dirname goes up one level from the location of the included file? So if the included file is in the location I'm looking to get to (myLoc), it's only one dirname(), but if it's in myLoc/sub1/sub2/sub3, then I'd use dirname(dirname(dirname(dirname(__FILE__))));? So essentially the number of dirnames allows me to always start from the location I'm wanting to start from for the ABSPATH? – Joshua Cody Feb 21 '10 at 08:32
  • Great. I included it in a constant file which is required within my header, which is included on every page. Works great on dev, and I expect it to work perfectly on production; about to svn it up to there. – Joshua Cody Feb 21 '10 at 08:46
  • I got an error and the page wouldn't even load. However I needed to make a small adjustment. If you universal php file is not in the root directory and you want to get the absolute path to the root, use: define('ABSPATH',dirname(dirname(__FILE__)) . '/'); – thethakuri Jun 26 '15 at 16:26
  • Also, if you want to use it inside quotations you have to use $, eg. echo 'Text text '.$ABSPATH.' text text' – thethakuri Jun 26 '15 at 16:32
1

declare below line in any of root file (index.php)

$_SESSION["uploads_base_url"]=dirname(__FILE__); 

and you can now use this in any of file where uploads needed.

echo $uploads_base_url=$_SESSION["uploads_base_url"];
Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
  • but here is a catch, becasue now you have tu use session_start() on every page.... better solution wouldn't be to use GLOBALS? – Mochi Apr 22 '21 at 09:24