Php document root

How to Include a File from Document Root in PHP

 


 

Truth be told, it happens sometimes that I want to make a website easier to be coded and I fail. Sometimes I face impassable barriers, or even levitate in vicious circles. Sometimes I feel like a monkey, and my brain is floating on the edge of darkness. I’m just sitting there waiting for the light but nobody but me can achieve that feeling of success. Yes, most of us are stars behind a cloudy sky.

 

PHP include document root

The most important principle of PHP is to know where we are! While including, it’s very important to refer the file from document root.

In html you type this for an image:

In php you type this to include a file:

But you know what the problem is with the php? That’s right, the path is relative. The html gives exact match but the php tries to swarm the file where the file contains the include code. Your actual php file which tries to include custom_file.php is in your root and the custom_file.php is in yourdomain.com/custom_codes/ directory.

So the solution is to give precise path to the custom_file.php.

 

 

The code is:

You can figure out your document root with the following:

It shows you the document root of your domain.

Why does it work differently? Because it can see your server directory structure on deeper level than html does.

This is the funny moment when I recognize that I don’t use a common way of troubleshooting. It’s very annoying when I don’t get some essential information written because of laziness and I’m just trying to get the code executed.

 

 


 

Posted by:

Balazs Hende
I am a blogger and the founder of this site. I have started sparesome.com on October 1, 2013. Have a pleasant stay here!

Related articles

Categories

Back to Top