Jump to content

Including Files from Varying Locations


ShadowMage

Recommended Posts

Is there a good way to include a file in another included file from other files that are in varying locations? I think I might have confused myself with that one. :)What I mean is, say the file I want included is in /webroot/includes/includedFile.phpand in that file I include /webroot/classes/classInclusion.phpI want to include this file in the following files:/webroot/index.php/webroot/forms/formOne.phpSo, in index.php I include includedFile.php, and in includedFile.php I need to include classInclusion.php so I can use include('includes/includedFile.php') in index.php and include('classes/classInclusion.php') in includedFile.php to include these files from index.php.Now I need to include it from formOne.php, but since relative file paths are relative to the file doing the inclusion and /webroot/forms/classes/classInclusion.php does not exist, I get an error. So what I did was set up a variable in includedFile.php to hold the relative path of the inclusion. So to include from index.php I'd do something like this:$path = 'classes/';include('includes/includedFile.php');and to include from formOne.php I'd do something like:$path = '../classes/'include('includes/includedFile.php');Then in includedFile.php I have this line to include classInclusion.php:include($path.'classInclusion.php');Is there a better way to do this? (That is if anyone understood what I just said :))

Link to comment
Share on other sites

would include_once in all places suffice? I'm not completely sure of the scenario, that's why I'm asking...

Link to comment
Share on other sites

I'm not certain how include_once would change anything. Let me try to explain a little better.From index.php I include a file (lets call it creator.php) which is located in the includes folder. creator.php creates an object. The file (let's call it object.php) defining the object, however, is located in the classes folder and is included in creator.php. Both the includes folder and the classes folder are in the root folder.I also need to include creator.php from other files which are located in different places so the relative path to object.php is different from each of these files. For example, the path from index.php would be classes/object.php where as the path from forms/form.php would be ../classes/object.phpWhat I want to know is if there is a better way to include creator.php from these differing locations.Did that clear it up?

Link to comment
Share on other sites

AFAIK, relative paths are usually relative to the CWD. You can make them relative to the file's location (regardless of CWD) by using dirname(__FILE__).I'm not sure if that's essentially what you're asking.

Link to comment
Share on other sites

It means Chronic Wasting Disease, it's nothing to joke about. He's probably talking about the current working directory, though. Unless he lives in Dallas, and he's referring to Community Waste Disposal.
Suggestion: Try "current working directory". It appears to be the most appropriate of the three. Justsomeguy has his bad days, just like all of us. Really, he is agoodguy, though.Roddy
Link to comment
Share on other sites

It means Chronic Wasting Disease, it's nothing to joke about. He's probably talking about the current working directory, though. Unless he lives in Dallas, and he's referring to Community Waste Disposal.
:)
AFAIK, relative paths are usually relative to the CWD. You can make them relative to the file's location (regardless of CWD) by using dirname(__FILE__).
I'll play around with dirname and see if I can figure anything out. I know my explanations aren't very good, so thanks for the effort, boen. :)And thanks to the rest of you for clearing up the CWD confusion. (Well, actually I think JSG might have added to the confusion...:) )
Link to comment
Share on other sites

Setting up the include path is the easiest solution.Also, you might want to check out the autoload function: http://www.php.net/manual/en/language.oop5.autoload.php
I can't change the include path because I need to include files from that location as well. Thanks for the suggestion though.It's just a very complicated system, so I think my current solution is probably going to be the best way, though I haven't had a chance yet to try out the dirname function that boen_robot suggested.
Link to comment
Share on other sites

You can set up the include path to search the local directory first, then others.For example:ini_set('include_path','.:..:/webroot/classes:/webroot/includes:/lib');You may also want to consider using .htaccess files to set the include paths, although I don't know what effect that would have on files that were read from files in different directories.Good luck.

Link to comment
Share on other sites

You can set up the include path to search the local directory first, then others.For example:ini_set('include_path','.:..:/webroot/classes:/webroot/includes:/lib');
Oh yeah! I forgot you could have multiple paths. I'll try that, thanks!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...