Django Anonymous
Django Anonymous

Reputation: 3025

How absolute path can be changed dynamically?

This is what we type for Absolute path:-

echo $_SERVER['DOCUMENT_ROOT'];

Output is the following:-

C:/xampp/htdocs

Currently i am working in a folder naming "project", Therefore my project root is:-

echo $_SERVER['DOCUMENT_ROOT'].'/project';

Output will be

C:/xampp/htdocs/project

Now if i upload my project to web-server with a folder name, say "website" i need to change the

echo $_SERVER['DOCUMENT_ROOT'].'/project';
------------------------------------------
to
------------------------------------------
echo $_SERVER['DOCUMENT_ROOT'].'/website';
------------------------------------------
or if i upload it in root then
------------------------------------------
echo $_SERVER['DOCUMENT_ROOT'];

I need to store files on different folders and directory levels.... so is there any way to make this thing dynamic any trick?

I want to minimize that hurdle and as renaming it all the time may can cause lot of time waste.

Once i used /project then /website and lastly directly to root... may be any trick through which we can bypass this hurdle and kind of declaration or stuff.. So that wherever we upload we just change a name in file and its done.... something like it.

Upvotes: 0

Views: 123

Answers (2)

scibuff
scibuff

Reputation: 13755

You could use something like this

$environments = array(
    'localhost' => 'development',
    'example.com' => 'production',
);

$active_environment = 'development';

foreach( $environments as $key => $value ){

    if( stristr( $_SERVER['SERVER_NAME'], $key ) ){
        $active_environment = $value;
        break;
    }
}

define( 'ENVIRONMENT', $active_environment );

    function getRoot(){

        switch( ENVIRONMENT ){
            case 'production' : return $_SERVER['DOCUMENT_ROOT'].'/website';
            case 'development' : 
            case default: return $_SERVER['DOCUMENT_ROOT'].'/project';

        }
    }

Upvotes: 0

Artjom Kurapov
Artjom Kurapov

Reputation: 6155

Initialize it in root dir with

dirname(__FILE__)

This will not work as you expect if you use linked directory (original path will be used), which is a bummer in CodeIgniter for example

Upvotes: 3

Related Questions