0

I have an include 'functions.php' which holds things such as connection details and various custom functions.

It works perfectly with every page but i just created a new page in a new dir, adjusted the include link accordingly, and the page is correctly included.

BUT, when using any of the functions from the include it breaks the page.

 include '../../../functions.php' 

Is this an acceptable way of linking to a a functions include? Is this why its broken? I normally use this method of linking and never had an issue, but today its not worked and has really rather puzzled me.

It works perfectly until i attempt to call any of the functions within the functions include.

example of a simple function that breaks the page

 function functionsTest() {
        echo "functions.php Included Correctly! ";
    } 

The functions page works perfectly across the rest of the site, and has only broken on this newly created page.

Any advice appreciated here thanks

3
  • 2
    Don't use relative paths for includes Commented Mar 26, 2014 at 16:02
  • how should i link to them, direct links? Commented Mar 26, 2014 at 16:03
  • @StuartWickenden use absolute, or real path Commented Mar 26, 2014 at 16:05

2 Answers 2

1

Relative paths in PHP are not relative to the physical file where the code is. Instead, they are relative to the working directory which, by default, is the directory of the main script, although you can even change it on runtime:

bool chdir ( string $directory )
Changes PHP's current directory to directory

If you need paths that are relative to current file, you have to build an absolute path yourself:

include(__DIR__ . '/foo.php');
Sign up to request clarification or add additional context in comments.

1 Comment

i solved with this include($_SERVER["DOCUMENT_ROOT"] . "/home/functions.php"); Many thanks
1

Say if you have a folder called myfunctions you can use real path like this:

define('FUNCTION_PATH', realpath(dirname(__FILE__).'/myfunctions'));

Then you would use that constant path like this:

include FUNCTION_PATH."functions.php"; 

2 Comments

is this acceptable? include($_SERVER["DOCUMENT_ROOT"] . "/home/functions.php");
@StuartWickenden no don't use $_SERVER["DOCUMENT_ROOT"] it wont work always

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.