Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include inside an include (different directory)

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.

like image 836
kilrizzy Avatar asked Dec 11 '09 14:12

kilrizzy


1 Answers

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";
like image 89
nickf Avatar answered Nov 03 '22 04:11

nickf