Jump to content

include() something in a different directory


mrbuter

Recommended Posts

Alright I thought this would be easier but I can't seem to find it through google :\

 

I have an includes folder with a config.php and connection.php for mysql. Anything in the root directory can include that fine (include("/includes/connection.php");) and anything in the includes folder can as well (include("connection.php");)

 

However, I don't know how to get anything in a DIFFERENT folder to include something in the includes folder.

 

for example,

 

www.mysite.com/folder1/index.php can't include www.mysite.com/includes/connection.php.

 

I could do include("http://www.mysite.com/includes/connection.php"); but I am making something which I am going to give to a couple of friends so having them edit a bunch of files to reflect their own url would be silly.

 

There _HAS_ to be a way to make php jump to the root public html directory isn't there?

 

I was looking at some vbulletin code and they do this by doing stuff like

 

require_once(DIR . '/includes/functions_databuild.php');

 

but how do they define DIR?

 

 

Link to comment
Share on other sites

The DIR is a defined constant that the code or a configuration file sets up.

 

However, I recommend using $_SERVER['DOCUMENT_ROOT'] for all include/require files so that you can reuse code in different paths and never have to worry about being able to get a relative path to work. The following will work (assuming the includes/connection.php is correct) no matter what file contains the include() statement -

 

include($_SERVER['DOCUMENT_ROOT'] . "/includes/connection.php");

 

Including a file using a URL (http:/www.yourdomain.com) will only include content that is output to the browser and does not actually include php code/data.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.