1

I am developing a website on a local test environment. From time to time I need to import classes or functions into my php pages. At first I used relative paths to import files, but for some (unexplicable) reason the PHP couldn't find those files. So I have thought it would be necessary use the following:

<?php require_once($_SERVER['DOCUMENT_ROOT'] .'/mywebsite/inc/libs/functions.php'); ?>

The problem is when I have to upload all the pages to my remote webserver, the PHP interpreter won't find functions.php in that position because there is no mywebsite subfolder , on the other hand I can't get rid of mywebsite subfolder, because that would leave me with http://localhost/inc/libs/functions.php which leads nowhere.

So that basically means I will have to manually readjust the path to make everything work. My question is: is there a way for PHP to detect the exact folder of my website so that I don't have to change paths everytime I need to upload a php file to my webserver?

haunted85
  • 1,601
  • 6
  • 24
  • 40

2 Answers2

0

What is the reason behind impossibility of use relative path?

<?php require_once(dirname(__FILE__).'/inc/libs/functions.php'); ?>
Sobit Akhmedov
  • 392
  • 2
  • 8
0

Set the base folder with all the files you want as an include path in php.ini.

That way, when you use include () or require (), it will automatically look in the included path.

www.php.net/manual/en/ini.core.php#ini.include-path

Phil Cross
  • 9,017
  • 12
  • 50
  • 84