php - Getting the project root -


i keep getting confused when trying include files have different paths in local environment vs production.

for example, have in usercontroller.php file:

require_once(__dir__ . '/config.php'); 

on local host, usercontroller.php located in www/myproject/inc/ , config.php in www/myproject/ (the project root directory)

this fails.

all want way define projects root. i.e on localhost it's www/projectname , in productions it's / since files located inside projectname directory on localhost, it's causing issues on production server.

what best way define base path , build require_once that?

i.e. require_once($basepath . 'inc/filename.php'

most projects use config.inc.php file located in common place these things can defined.

config.inc.php

define('root_folder', '/'); define('application_link', 'http://www.example.com/mysite'); 

script.php

require ('../config.inc.php'); echo '<a href="' . application_link . '">go home</a>'; 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -