Jump to content

[SOLVED] includes question?


schilly

Recommended Posts

Ok weird situation here.

 

Here is my file structure:

 

admin

-->index.php

-->modules/

    -->mymodule/

        -->index.php

        -->includes/

        -->views/

 

So everyone is loaded off the admin index.php page and it includes different modules depending on the variables provided.

 

So here's the problem. I'm trying to include a file in includes from a file in views and

 

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

 

wont work. If I include from the mymodule index.php (includes/includes.php) it works but the include file is all html so I didn't want to have to wrap it in a variable to use it in the views/ script.

 

So I ended up doing some debug and found the executing directory to actually be the admin directory which makes sense because that's where the original request is coming from. Switch my include to (modules/mymodule/includes/includes.php) and it works.

 

Now here's my question. Why would includes/includes.php work in the mymodule index but the ../includes/includes.php not work in the views/ page? Is there something about the file system that breaks when you try to go backwards in a directory?

 

Hopefully this I made this clear.

 

Thanks.

Link to comment
Share on other sites

Relative include paths (those with ../ or ./) are relative to the current working directory (CWD). The starting CWD is that of the main script file that is including all the other files, not a file that has already been included at a path different than the main script file and wants to include yet another file.

 

You can either use absolute file system paths, form more complicated relative paths ../../, change the CWD (and remember to change it back as necessary), or setup and use the include_path and let php search for the files to include (slower than if you don't use the include_path.)

 

 

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.