Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

root path for php files in Linux apache

I am having problems with locating files with php include on my Ubuntu server.

structure of site

/var/www/
     home/index.php
     include/header.php

When I try to insert the following include_once('/include/header.php') in the file home/index.php it does not work.

however if I change that to include_once('../include/header.php'), it works fine, but to have consistency through out my site I can't allow this

What is the best way to fix this?

like image 411
mk_89 Avatar asked Jul 19 '26 09:07

mk_89


2 Answers

If your document root is /var/www/ then you can use:

include $_SERVER['DOCUMENT_ROOT'] . '/include/header.php';

Typically, PHP gets the DOCUMENT_ROOT correct so you can usually rely on that.

If you want to include the file relative to the script that is doing the include, you can do:

include dirname(__FILE__) . '/../include/header.php';
like image 107
drew010 Avatar answered Jul 21 '26 00:07

drew010


The leading slash indicates an absolute path. Most applications will define an APP_ROOT constant or something similar early in the cycle to solve these problems.

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

Then later on you can include files with:

include(APP_ROOT . '/includes/header.php');
like image 33
Mike B Avatar answered Jul 20 '26 23:07

Mike B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!