0

echo __DIR__;

" How to get document root path in PHP from any subdirectory using command line?

my directory structure--

/dinesh
       /data
            /asset

Suppose my root directry is /dinesh

and currently my script in asset but I require document root path till /dinesh only

by using command line php CLI

How will I get this?

I have tried DIR and FILE but that give path till asset and one more thing that dinesh is my local root folder name that is changeable when I move my code to other server"

CBroe
  • 91,630
  • 14
  • 92
  • 150
Dinesh Ghule
  • 3,423
  • 4
  • 19
  • 39

4 Answers4

3

echo($_SERVER["PWD"]); its working

Dinesh Ghule
  • 3,423
  • 4
  • 19
  • 39
1

try the code to get directory in php

dirname(__FILE__);
Praveen P K
  • 198
  • 12
  • take a look in this url: https://stackoverflow.com/questions/1894917/how-to-get-the-home-directory-from-a-php-cli-script – Praveen P K Jun 01 '17 at 11:47
0

In CLI mode there is no "root directory". If you do

print_r($_SERVER);

you'll see that "DOCUMENT_ROOT" is empty. What you can use is

__DIR__; //asset

That will give you the path of the script, so you can go up any levels to get your "root directory" doing

dirname(__DIR__); //one level up 'data'
dirname(dirname(__DIR__); //two levels up 'dinesh'

But php cli won't know by himself what you are considering root directory because that's a constant of the web server.

lisandro
  • 454
  • 4
  • 12
-1

You can print the result that comes out of the global variable

echo $_SERVER["DOCUMENT_ROOT"];

And print

echo @getcwd();