View Full Version : PHP include paths

05-01-2009, 07:33 AM
If I were to use a PHP include in my site, such as:

<!DOCTYPE ...>
<?php include('/path/to/include.ext');?>

Can I use an absolute path instead of a relative one? On my site now I use the code:

<?php include('includes/nav.html'); ?>

Which will be fine until I ove a file up or down a level. I have found several topics on the web about this issue but I am new to PHP and have no friggin idea what they are talking about.

Is an absolute path possible?

05-01-2009, 09:17 AM
I think you misunderstand. The first block of code is an absolute path (starts with / for root), and the second is a relative to the current script.

My recommendation is to always use relative where you can. But, be aware that PHP takes its cwd (current working directory) from the executing script, not the included ones.

+ Users/
+---+ YourSite
+-----+ public_html
+---- index.php
+ includes
+---- menu.php
+---- TemplateManager.php

Now, if you include menu.php into index.php using include 'includes/menu.php'; and the same with templateManager, then use templateManager in menu.php like so: include 'TemplateManager.php';, this will fail. This is because the cwd of the index.php is public_html, while the menu cannot find the TemplateManager.php. Solve by creating a relative absolute path (sorry, I don't have a better name for it). This lets you attach to any file relative to the current file:

require_once dirname(__FILE__) . '/./includes/TemplateManager.php'; // Index's call
require_once dirname(__FILE__) . '/./TemplateManager.php'; // Menu's call

05-01-2009, 09:51 AM
I should clarify again, here is an example of what I mean:


In this example, my source code is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php include('http://www.temp1.hangnailproductions.com/includes/nav.html'); ?>

But that include address is failing.
Does that make sense?

The reason I ask is because if I base every page off a template with the same include, but save the additional files in a subfolder, I don't think the include will work...

05-01-2009, 11:40 AM
i think allow_url_include is not true.

have you read what Fou-Lu wrote???
the "/" before files\folders will get you absolute within your web.
include '/includes/nav.html';

05-01-2009, 01:08 PM
<?php include('http://www.temp1.hangnailproductions.com/includes/nav.html'); ?>

If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see List of Supported Protocols/Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
You don't actually need to include your file via HTTP ,which is not equivalent of including the path at the server side.

If you want to use the absolute path at the server side, use it like

include $_SERVER['DOCUMENT_ROOT']."/includes/nav.html";
(just echo the above serve variable to know what's your absolute file path to your roo directory at server side)

I usually define a constant like

define(_ROOT_, $_SERVER['DOCUMENT_ROOT']); at the top(or in config file, which would get included in all other files), to make it easier for typing the absolute path, like

include _ROOT_."/includes/nav.html";