Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4607434
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:42:10+00:00 2026-05-22T00:42:10+00:00

I just define a root path as a constant like this define(ROOT_PATH, dirname(__FILE__)); so,

  • 0

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";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:42:11+00:00Added an answer on May 22, 2026 at 12:42 am

    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 or required 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:

    define('ROOT_PATH', dirname(__FILE__));
    

    In somedir/somefile.php:

    require_once "../utility.php";
    echo ROOT_PATH;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

config.php put at the root level, this file will be included in any pages.
echo '<img src=../../images/delete.png id=aaa />aaa&nbsp;&nbsp;&nbsp;'; (working fine) //define( 'ROOT_DIR', dirname(__FILE__) ); is in a
We can modify the document root directory path for PHP using $_SERVER['DOCUMENT_ROOT'] = to/some/new/directory;
I want to define some member variable and some code just in Debug Mode,
I found this thread on StackOverflow about how to get the Application Root from
I wrote this small bit of code to write to a plist file. I
Is that possible to define a function without referencing to self this way? def
This question has been asked before - How we can use CTE in subquery
I like to refer images, css and php files using their absolute path, that
How do I define schema for the following XML: <root> <elements> <element1/> <element2/> <element1/>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.