EllaJo
EllaJo

Reputation: 235

include into subdirectory without knowing the path of file to be included

I have a site with multiple directories and subdirectories. I am only working withing one directory: framework, and all its subdirectories. So I am working with /framework, /framework/Module1, /framework/Module1/Class1, and on down to framework/Module1/Class1/biology/microscope/microscopeTutorial/thispage.php. I don't know the path to get to framework.

I need to put an include into my files that will include a file inside framework, but I want to be able to use the file. I have found two options. $_SERVER['DOCUMENT_ROOT'] doesn't work because I'm not in the root directory. And plugging the appropriate number of ../../../ manually into more than a thousand pages seems clunky, frustrating, and a waste of time.

I have written a script that I can run from within Framework that will iterate through all the directories and subdirectories that will add the line include('header.php'); into all the files. Editing this script to be appropriate for each subdirectory is nearly as frustrating as adding them all in manually.

So how do I tell all the files between framework and framework/Module1/Class1/biology/microscope/microscopeTutorial/ to look for the file in framework?

I guess what I need is a single line that says, keep looking up until you find it, and then stop. Does such a thing exist?

Upvotes: 0

Views: 274

Answers (2)

EllaJo
EllaJo

Reputation: 235

I tried a couple different variations on the answer Ian gave, and tried some variations using dir functions. I finally decided to just create a new copy of my file, add the extra ../ in to the pertinent part of code, and run that in the subdirectory.

Thanks for the help!

Upvotes: 0

Ian
Ian

Reputation: 2021

I think you need to install this in an __autoload function. __autoload(className) is called when a class is used that has not been loaded.

It could be something like this

function __autoload($className) {
    //  pick file up from current, up directory tree
    $fname = $className.'.php';
    while (!file_exists($fname) {
        $fname = '../'.$fName; 
    }
    require_once $fname;
}

Note - code untested.

Upvotes: 1

Related Questions