How can I manage DOCUMENT_ROOT to work on the local and real server?

To make URLs work in versioned projects, I used $ _SERVER ['DOCUMENT_ROOT']. The problem is that I am developing projects inside the folder, so I get the following:

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

When I go live, I usually just want the following:

$ _ SERVER ['DOCUMENT_ROOT']. '/path/to/file.php'

I know that there are big problems in the world than deleting and adding this folder name, but is there a way I can easily automate this? Can I somehow locally set the locator of my document to include the folder in which I work? Do I have a fundamental misunderstanding of how everything works? Something new in this material, and he wants to learn as much as possible and really lure the "why."

Many thanks!

+7
php localhost
source share
2 answers

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

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

Place the following code in the file located in the root folder of the application, and include it each time the page loads.

Then you can just do $path = ABSPATH . '/path/to/file.php'; $path = ABSPATH . '/path/to/file.php'; regardless of whether your local copy is in the subdirectory folder or not.


If your application already has a file that is included in each download on the page, you can simply delete the above code in this file and it will work.

Please note that you may need to add additional dirname() calls depending on where the file is located. Add one for each directory that you submit from the root of your web application.

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

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

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


Explanation

The above code is for one file and one file only in your web application. This file must be included for every page load.

If you already have a file that is included before any processing (for example, a configuration file or another), you can copy and paste this code into this file.

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

If you copy my code to ~/abspath.php , you will need one call to dirname() .

If you copy my code to ~/includes/abspath.php , you will need two calls to dirname() .

If you copy my code to ~/includes/config/abspath.php , you will need three calls to dirname() . Now let me say 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 will do the following:

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

and you have access to ABSPATH .

That's why I say that if you already have a file that is included in every page load, it is easier to simply drop the above code in it. Just make sure you adjust the number of dirname() calls accordingly. Again, this code is for ONLY ONE FILE.

+10
source share

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

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

and now you can use it in any file where downloading is required.

 echo $uploads_base_url=$_SESSION["uploads_base_url"]; 
+1
source share

All Articles