Jump to content

Recommended Posts

Just realize that burying a specific path inside a function limits its portability. If there has to be a path why not pass it in as an argument?

 

Or are you referring to the include STATEMENT, and not a user-written function? If so, I think you can answer that yourself.

Edited by ginerjm

There is no magical function for getting the project root, because PHP doesn't know that path.

 

What you can do is have the webserver set an environment variable with the project root (how exactly that works depends on your webserver; google it). PHP has then access to that variable:

require_once $_SERVER['MY_PROJECT_ROOT'].'/path/to/script.php';

Alternatively, you could define a constant with the path within PHP (e. g. in a configuration script). But that means you always need to load the initial script with an “ugly” path before you can use the constant for everything else:

<?php

define('MY_PROJECT_ROOT', $_SERVER['DOCUMENT_ROOT'].'/path/to/project/');
<?php

require_once __DIR__.'/../relative/path/to/config.php'
require_once MY_PROJECT_ROOT.'/path/to/some/script.php';    // now MY_PROJECT_ROOT is available
  • Like 1
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.