Jump to content

Include Paths


2bigpigs

Recommended Posts

I'd like to include a few files in my scripts.

On WAMP, I've been using the document root as include path but i don't know how to do it here.

Ideally, I'd have this:

<!--?php
require_once('global.inc.php');
require_once('myclass.inc.php');

Is there a specific folder that i should put my includes in?

 

I get this error:

Failed opening required '/global.inc.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in/home/bigpig/public_html/ajax/shelf.php on line 5

 

One more question: Will using too many includes (3) slow down execution by a lot?

Link to comment
Share on other sites

You can use include for files from different folders, but the whole path to the file must be included.

Example:

/home/bigpig/public_html/(path-to-file)

And yes, include does slow down execution time, depending on the amount of includes and the size/amount of code of the included files. I think it is best to limit using include to about 3 times.

Link to comment
Share on other sites

In PHP the logic is as following (source http://www.php.net/manual/en/function.include.php ):

 

* If the path starts with / then it is deemed the absolute path on the filesystem of the server

 

* If it does not start with ./ or ../ then it will look in the include_path. This is set to be . and two more system locations you cannot reach yourself.

 

* If the path does start with ./ or ../ then it is relative to the current file

 

I had some problems with included and then I started using

<code>require_once __DIR__.'/shared.php';</code>

Where __DIR__ is always the absolute path of the file in which it is used. So if you use __DIR__ in two files, the one including the other, then __DIR__ is (or can be) different for both files.

 

But prepending with ./ should be the same as __DIR__ so maybe I will give that another go myself (once I have something working I do not easily change) :-).

 

I would advice against using absolute paths: it would make it more difficult to move to a different server or subdomain.

Link to comment
Share on other sites

Thanks.

I was wondering whether heliohost had a preset include path for my files. If it did, Then i could easily move hosts or subdomains without having to worry too much.

Right now i have one global_config which is in the same directory as the pages + a classes folder.

 

Thanks for your support. I have my answer :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...