Reputation: 671
I am having a bit of trouble with relative path in php. Within one of my classes I have the following code:
$iniPath = $_SERVER['DOCUMENT_ROOT'] . "/movies/config.ini";
$ini_array = parse_ini_file($iniPath, true);
This code works perfectly because it returns "/var/www/movies/config.ini".
But the problem is the code isn't transportable. If its installed on another server they may not install it in /movies/.
tl;dr How do I specify application directory as root directory in php?
Upvotes: 0
Views: 190
Reputation: 7030
you can use __DIR__
or dirname(__FILE__)
to determine the current path relative to your application.
You can define a constant inside the first file called (or inside a config or bootstrap file) :
<?php
define('ROOT_DIR', __DIR__.'/');
// and define some other base path
// you can also define everything in a separate file
define('MOVIE_DIR', ROOT_DIR.'movies/');
// and define absolute path
define('CLASS_DIR', '/usr/local/share/something/that/will/never/change/');
// then your code will looks like
$iniPath = MOVIE_DIR . '/config.ini';
$ini_array = parse_ini_file($iniPath, true);
EDIT: an other alternative (after reading your comments in your question) is to use set_include_path but, that's not often used.
Upvotes: 1
Reputation: 513
Try to checkout the PHP magic constants
__DIR__
http://php.net/manual/en/language.constants.predefined.php
Upvotes: 0