Category Archives: absolute

Relative Path issue while using PHP Include in subdirectories

I am in the Process of Building a PHP Application. The application itself has large number of pages and hence I decided to categorize each files into corresponding Subdirectories..

However I have encountered a problem. All the template resources folders(CSS,JS, Images etc) are located in the root folder localhost/appname/. and also the Navigation Menu (navmenu.php). I am including the menu in each page.

The Problem arises when I try to include the navmenu.php inside the files in sub-directories.

ie when I include navmenu.php in index.php(Both in root folder) works great. But when i Include navmenu.php in addstudent.php located in student subdirectory all the a href links in navmenu.php becomes student/*.php (Relative Links)

What is the proper way to solve such Situations.. I have a configuration file which is included in all php files. How should I create a constant..

and how should I update the links in the navmenu.php href links.

Thanks in Advance

dirname(__FILE__) adds path after my domain

Here's my problem using the PHP function dirname()

Let me first explain, I'm working on a website where I defined a constant as follow :

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

That worked well on a shared hosting, but I had to move the website on a dedicated server and now some files I try to call with an absolute link return 404. When I look what path they use, something like this appears :

http://myIp/var/www/myWebsite/[...]/image.jpg

What happens is that, my 'ROOT' constant is '/var/www/myWebsite' but in place of 'Replacing' my domain, it's added at the end of it. Which makes no sense since my Apache VirtualHost sends all requests towards 'myIp' to the local folder '/var/www/myWebsite'.

I don't know if the mistake is either from my PHP code, or my Apache VirtualHost.

Thank you for your help, I'm sure it's something stupid but I can't figure what I did wrong :)