Sunday 2 June 2013
Facebook StumbleUpon Twitter Google+ Pin It

PHP include from root directory

PHP include from root directory

Some times you would have wondered how to include a file from root directory in page when you are in another directory. we have to give relative path in order to avoid errors if we transfer the page to another root.
normally we include a file like this
Because it doesn’t begin with a “/”, it suffers from the same problem our images example suffered from. If that include code moves to a different directory, the reference can be broken. But with PHP, simply using that beginning “/” will not work, which can be mighty confusing.
The problem is that PHP can see a bit “deeper” into your servers file system than HTML can. For example, the public web directory of voidTricks actually lives at “/var/www/voidTricks/” on my server. So when you do an include with a “/” at the beginning, it looks WAY down deeper than you are intending it to. You actually want it to look in that public web directory. In my case, “voidTricks”.
but if we want to include a file from root directory we need to automatically find out the root directory path. see the code below
That’s it.
You might be thinking that this is a simple trick. But this is a basic thing that every PHP programmer should know before doing a PHP project.
share this post if you like it. sharing the knowledge is better than sharing the wealth. Support us by sharing our posts. this will encourage us to post more tricks

No comments: