How to Use a PHP Includes Across Multiple Directories/Sub Directories with Relative Paths

How to use a PHP includes across multiple directories/sub directories with relative paths

i usualy have a file called config in my application root and in it i define a constant for base path and a few others:

define('APP_BASE_PATH', dirname(__FILE__));
define('APP_FUNCTIONS_PATH', APP_BASE_PATH . '/functions');

and i include my files like

include (APP_BASE_PATH . 'includes/another_file.php');
include (APP_FUNCTIONS_PATH . '/function_file.php');

that way i can place my aplication in whatever directory, plus i can move files around without to much worries.

also using full path makes the include faster

PHP include files from different subdirectories within the root folder

@sandeep's answer is partially right. because

$_SERVER['DOCUMENT_ROOT'];

will again give back fully qualified path to the root.

<img src=" <?php echo 'http://localhost/favicon.ico';?>"/>

will return image back because now I am not giving it local path as per your problem but url of the server I am running.

PHP include files from different subdirectories within the root folder

@sandeep's answer is partially right. because

$_SERVER['DOCUMENT_ROOT'];

will again give back fully qualified path to the root.

<img src=" <?php echo 'http://localhost/favicon.ico';?>"/>

will return image back because now I am not giving it local path as per your problem but url of the server I am running.

php includes is there any way to include a file relative only to that document?

This may help: (from http://php.net/manual/en/function.include.php)

Files for including are first looked
for in each include_path entry
relative to the current working
directory, and then in the directory
of current script. E.g. if your
include_path is libraries, current
working directory is /www/, you
included include/a.php and there is
include "b.php" in that file, b.php
is first looked in /www/libraries/
and then in /www/include/. If filename
begins with ./ or ../, it is looked
for only in the current working
directory or parent of the current
working directory, respectively

Your question states:

If I want to include another file
inside footer.php, I must do it
relative to the index.php file (the
one that is including it).

This is true only if the filepath you are trying to include() starts with ./ or ../ . If you need to include a file above the current file using a relative path, you can (as you suggested) use:

include( dirname(__FILE__) . '/../file.php')

If you define an absolute path, you can also add this to the current include_path:

set_include_path(get_include_path() . PATH_SEPARATOR . '/absolute/path');

You can then do all your includes relative to '/absolute/path/'.

Using php include with multiple sub directories

I have answered my own quest and feel silly for even asking the question. I just used absoulte paths

nav.php

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a href="http://domain.com/about.html">About</a>
</li>
<li>
<a href="http://domain.com/Services.html">Services</a>
</li>
<li>
<a href="http://domain.com/index.php">Contact</a>
</li>

<li>
<a href="http://domain.com/Login/Sign-In.html">Login</a>
</li>

<li>
<a href="http://domain.com/Login/logout.php">Logout</a>
</li>

<li>
<a href="http://domain.com/PHP/Cart.php"><img src="images/Cart.gif" style="height: 20px; width:20px;"> Cart</a>

</li>

</ul>

Relative Path issue while using PHP Include in subdirectories

For it to always be included no matter the directory depth try the following

include $_SERVER['DOCUMENT_ROOT'] . "/includes/navmenu.php";

This should return something like: /home/<user>/public_html/includes/navmenu.php

In majority of my projects; I have a settings file similar too:

$LiveDir = "http://localhost/project";
$RootDir = "/home/user/public_html/project";

This is primarily beneficial when changing hostname, as you only have to change the links in one file rather than going through ALL your pages and updating the links

<a href='<?=$LiveDir?>/some/directory/index.php'>Some Link</a>

I use a similar setup to work between my work and my home on a huge CRM. If I am at home links get prefixed with http://127.0.0.1/ when I'm at work it automatically converts to http://192.168.10.23/Matilda

Caution: $_SERVER does not get populated during a cron job. A work around:

Create a file: CronMgr.php (for example) with the contents and direct your cron job to it.:

<?php file_get_contents("/full/path/to/the/script.php"); ?>

This will open an instance where $_SERVER is populated and the script will run without issue.

How get nested, relative includes to work?

I just need to add the path like this (and then reset it after):

//This is so we don't screw up anything else in the PHP web app
$currentIncludePath = get_include_path();

//Need to let phpseclib know where to find its files
set_include_path( "phpseclib" . PATH_SEPARATOR . $currentIncludePath );

//Now include the file(s)
include( "phpseclib/File/X509.php" );

//Now set back to normal
set_include_path( $currentIncludePath );


Related Topics



Leave a reply



Submit