Jump to content

[SOLVED] confused about path of included file in included file


Recommended Posts

Hi,

 

I have a file that includes another file which contains some query information. The query info file needs to include the connections file. This is fine if I keep my site in a  two-tiered schema: root/folders -- I just use '../folder/filename.php in every path. But I get confused if I try to organize anything in subfolders.

 

I've avoided using subfolders for the most part for this reason, but the site is getting big enough that I think I'm going to have to start.

 

for example:

 

I have a file that references a source file so I keep the source files in a folder called sources. now I want to create a subfolder called sources because the main sources folder is too big and cumbersome.

 

file: report1.php

path: ../reports/report1.php

source path: ../reports/sources/report1_source.php

connection path: ../connections/conn.php

 

then in the source file I include the connection file:

require_once('../../connections/conn.php');

 

which is fine with that file (the report source file) but then when that file gets included down path, the path no longer works: Warning: require_once(../../connections/conn.php) [function.require-once]: failed to open stream: No such file or directory

 

sorry if I'm not explaining this problem really well.

Then don't use relative paths (i.e. the path to the file from the location of the current file). Instead use absolute paths from the root of your site.

 

I'm having trouble following your example. But let's go with this:

 

file: report1.php

path: ../reports/report1.php

source path: ../reports/sources/report1_source.php

connection path: ../connections/conn.php

 

In the file report1.php you can include the source using this:

$_SERVER['DOCUMENT_ROOT'] . "/reports/sources/report1_source.php";

 

And that file can include the connections file using:

$_SERVER['DOCUMENT_ROOT'] . "/connections/conn.php";

 

It doesn't matter what directory any file calls the connection file from. It will always exist at the same absolute path.

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.