I just define a root path as a constant like this
define("ROOT_PATH", dirname(__FILE__));
so, at the same file, they can recognize ROOT_PATH, however, in the other file, ROOT_PATH is null, I check some forums, people said defined constant can be use in global, however, I can’t, is that I need to do some config in PHP.ini?
Actually, why I do that is because I’m moving the site to new server, however, the new server seems don’t like me use relative path, therefore, I need to change to absolute path.
UPDATE:
Thanks everyone, I think I catch the reason, I use ajax to call the file, so the ROOT_PATH is null, therefore, I define ROOT_PATH in every file, but another problem is appear, my file structure is sth like this
/index.php
/ajax/a1.php
/inc/inc.php
index.php:
<?
define('ROOT_PATH',dirname(__FILE__));
include_once ROOT_PATH."/inc/inc.php";
?>
a1.php:
<?
define('ROOT_PATH',dirname(__FILE__));
include_once ROOT_PATH."/inc/inc.php";
?>
so, when the client side call a1.php, it will include inc.php, however, it doesn’t like what I imagine, I get this from the chrome developer tools
<b>Warning</b>: include_once(D:\.............\ajax/inc/a1.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in <b>D:\..................\inc\inc.php</b> on line <b>3</b><br />
I check with the ROOT_PATH generated by both file, find that, FILE is depends on the caller,
if index.php call it, the
ROOT_PATH = d:\.............\
if a1.php call it, the
ROOT_PATH = d:\..............\ajax\
however, both file I need to call that file, any method can I overcome this? thanks.
SOLVED:
Hi, I just figure out how to over come the path problem, first of all, we need to setup a file in every directory, and this file store the absolute path information refer to the relative path(hard to understand? :P), I call this file path.php, for my example
/index.php
/ajax/a1.php
/inc/inc.php
therefore, we need to add 3 path.php in this 3 directroy, as follow,
/index.php
/path.php
/ajax/a1.php
/ajax/path.php
/inc/inc.php
/inc/path.php
each setting of the path.php like this,
/path.php
<?
if(!defined(ROOT_PATH))
define(ROOT_PATH, dirname(__FILE__));
?>
/ajax/path.php & /inc/path.php
<?
if(!defined(ROOT_PATH))
define(ROOT_PATH, '..');
?>
so, if we have a 3 leve structure, let say: /one/two/, then the /one/two/path.php should be like this
<?
if(!defined(ROOT_PATH))
define(ROOT_PATH, '../..');
?>
After we set up all this file, we need to include this path.php as follow, for example with index.php
/index.php
<?
include_once "path.php";
include_once ROOT_PATH."/inc/inc.php";
...
?>
So, whoever the caller is, it will first include the local path.php, and find out the relative path (/ or .. or ../..), therefore, the included file will not be wrong because all of them are include with correct symbol, let say,
if caller is /index.php, then ROOT_PATH = “FULLPATH”, so
include_once ROOT_PATH."/inc/inc.php";
=
include_once "FULLPATH"."/inc/inc.php";
if caller is /ajax/a1.php, then ROOT_PATH = “..”, so
include_once ROOT_PATH."/inc/inc.php";
=
include_once ".."."/inc/inc.php";
I think you’re misunderstanding what “global” means.
A constant is available in a script, from the point you define it until the end of the script, including in the context of any scripts
included orrequired inside it.It’s not magically available in other, unrelated scripts on your webserver.
Update
So you’re trying to obtain the path to your webroot from any arbitrary file underneath it.
Of course
__FILE__won’t work, as an individual file knows only its own path, and not how far down the directory structure it is.You could try this:
In
utility.php:In
somedir/somefile.php: