3

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?

3
  • the absolute path will always work include_once('/var/www/include/header.php') Commented Sep 12, 2012 at 21:01
  • @Dagon if I do that I won't be able to test it in xampp Commented Sep 12, 2012 at 21:02
  • well now you tell me you want that, do i look psychic :-) Commented Sep 12, 2012 at 21:03

3 Answers 3

5

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';
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks this works, it's only a shame there's no dreamweaver user friendly solution
Why doesn't dreamweaver like it?
it doesn't let you preview php files from the top file navigation, but I guess it's not a big deal
2

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');

Comments

1

I would try the following

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.