Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an absolute include path in PHP?

Tags:

php

In HTML, I can find a file starting from the web server's root folder by beginning the filepath with "/". Like:

/images/some_image.jpg

I can put that path in any file in any subdirectory, and it will point to the right image.

With PHP, I tried something similar:

include("/includes/header.php");

...but that doesn't work.

I think that that this page is saying that I can set include_path once and after that, it will be assumed. But I don't quite get the syntax. Both examples start with a period, and it says:

Using a . in the include path allows for relative includes as it means the current directory.

Relative includes are exactly what I don't want.

How do I make sure that all my includes point to the root/includes folder? (Bonus: what if I want to place that folder outside the public directory?)

Clarification

My development files are currently being served by XAMPP/Apache. Does that affect the absolute path? (I'm not sure yet what the production server will be.)

Update

I don't know what my problem was here. The include_path thing I referenced above was exactly what I was looking for, and the syntax isn't really confusing. I just tried it and it works great.

One thing that occurs to me is that some people may have thought that "/some/path" was an "absolute path" because they assumed the OS was Linux. This server is Windows, so an absolute path would have to start with the drive name.

Anyway, problem solved! :)

like image 604
Nathan Long Avatar asked Dec 05 '08 16:12

Nathan Long


People also ask

How do you specify an absolute path?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).

What does include () do in PHP?

PHP Include Files. The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

What is include path in PHP?

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.


3 Answers

What I do is put a config.php file in my root directory. This file is included by all PHP files in my project. In that config.php file, I then do the following;

define( 'ROOT_DIR', dirname(__FILE__) ); 

Then in all files, I know what the root of my project is and can do stuff like this

require_once( ROOT_DIR.'/include/functions.php' ); 

Sorry, no bonus points for getting outside of the public directory ;) This also has the unfortunate side affect that you still need a relative path for finding config.php, but it makes the rest of your includes much easier.

like image 103
Rob Prouse Avatar answered Sep 21 '22 18:09

Rob Prouse


One strategy

I don't know if this is the best way, but it has worked for me.

$root = $_SERVER['DOCUMENT_ROOT']; include($root."/path/to/file.php"); 
like image 38
Nathan Long Avatar answered Sep 22 '22 18:09

Nathan Long


The include_path setting works like $PATH in unix (there is a similar setting in Windows too).It contains multiple directory names, seperated by colons (:). When you include or require a file, these directories are searched in order, until a match is found or all directories are searched.

So, to make sure that your application always includes from your path if the file exists there, simply put your include dir first in the list of directories.

ini_set("include_path", "/your_include_path:".ini_get("include_path"));

This way, your include directory is searched first, and then the original search path (by default the current directory, and then PEAR). If you have no problem modifying include_path, then this is the solution for you.

like image 42
gnud Avatar answered Sep 20 '22 18:09

gnud