5

The structure for the current project I am working on is something like:

  • root/index.php
  • root/includes/php/first.php
  • root/includes/php/functions.php

So index.php includes first.php:

<?php include_once("includes/php/first.php"); ?>

Normally then my first.php would call functions like:

<?php include_once("includes/php/functions.php"); ?>

Assuming the relative would still be from the index page however when moving to a new server it didn't work. I tried making the relative path from first.php:

include_once("functions.php");

And this seems to work now.

Which would normally be the correct way to do this? I want to roll this project out so just about anyone would be able to install this.

2 Answers 2

12

includes are relative to the file doing the including. If you really want to make sure and avoid any ambiguity, you could do this:

include dirname(__FILE__) . "/functions.php";
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome thanks! Not sure why it worked before the other way but this solution will solve future issues
+1 - I was going to suggest using the $_SERVER['SCRIPT_FILENAME'] element to do the same, but this is much better.
I tend to create a couple of definations for my PHP app's and store my root_path and web_path there for use throughout
I heard that php didnt do preporcessing on file includes and are instead executed when that statment is reached.... but how can it be relative to the file doing the including and not be doing some preprocessing if that file is say included in another file thus changing the directory model?
0

I always use:

include ($_SERVER['DOCUMENT_ROOT']."/include/php/functions.php");

This way you always know where you are (coming from the root) and your code can be copied from place to place.

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.