?
Solved

PHP include paths

Posted on 2004-12-01
6
Medium Priority
?
215 Views
Last Modified: 2013-11-18
More of a general one this. I've got a bunch of classes that I use day to day. I've been playing with one of them, and I have tried to include another file at the top of the class containing some configuration options.

My path is like this:

/root/index.php

root/includes/class.inc.php
root/includes/config.inc.php

So, the index includes the class, and the class includes the config.inc.php.

However, am I right in thinking, that for the class to include the config.inc.php, it will start the path from where *it* was originally called? i.e if my class file simply has include('config.inc.php'); it will in fact try to call it in from the root directory, not where the class itself is stored?

What's the most flexible way around this?
0
Comment
Question by:andycal
6 Comments
 
LVL 9

Expert Comment

by:gruntar
ID: 12714088
you can include a file that is not in a root dir where index.php is like this

include("includes/config.inc.php");

Cheers
0
 
LVL 48

Expert Comment

by:hernst42
ID: 12714111
if you want to include a file once use
require_once or include_once, so such a php-file is only included once, even if its called with require_once multiple times.

I typical have a fill which includes all nexessary thing e.g

config.php
<?php

// make settings
....

//include classes and function

require_once 'includes/class.inc.php';
require_once 'includes/class2.inc.php';
....

on the pages i use the only do a
require_once 'config.php';
0
 

Author Comment

by:andycal
ID: 12714157
That's kinda relying on knowing always the path, I guess what I'm after is a way of ensuring that the config file is called into the class file, always using the class file path.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:nicholassolutions
ID: 12714164
you can also set your include_path directive in the php ini file such so that PHP searches for included files in the way that you want. You can do this in your script at runtime using ini_set():
http://us2.php.net/manual/en/function.ini-set.php

For information on include_path:
http://us2.php.net/manual/en/ini.sect.path-directory.php#ini.include-path

From the PHP manual:
Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is ., 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/ and then in /www/include/.

Cheers,
Matt
0
 
LVL 9

Expert Comment

by:gruntar
ID: 12714183
To this you just have to set up include path, where your includes reside like this
include_path=".:/root/includes"

then you include those files just like if they were in root
include('config.inc.php');
That way your include will be the same no matter in what direcory file is.

Cheers
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 375 total points
ID: 12714220
You can use the statement below in your class.inc.php to always include the config correctly, even when you are not sure about the original basepath:

include_once(dirname(__FILE__).'/config.inc.php');

__FILE__ contains the actual absolute path to /root/includes/class.inc.php, therefor dirname(__FILE__) returns /root/includes.

Even when you move the files, but they both stay in the same directory, this method still works.

Regards

-r-
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question