Get directory from server

Asked

Viewed 3,701 times

2

How can I get the directory where the server is located using PHP?

The directory of the server and not the file where the script

2 answers

6


I think this is what you want:

echo realpath(dirname(__FILE__));

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Documentation dirname() and realpath() and __FILE__.

The __FILE__ gives the file path, the dirname() takes only the directory ignoring the file name (can be done with any path since what it takes is the parent directory of the path past) and the realpath() takes the absolute path of the relative address found. You can use a combination of them according to the need.

5

This information may be obtained through array $_SERVER passing as argument DOCUMENT_ROOT.

DOCUMENT_ROOT: The root directory under which the current script runs, as defined in the server configuration files.

echo $_SERVER['DOCUMENT_ROOT'];
  • 2

    +1 for doing what the question asks

  • 1

    @Gabrielhenrique strange, because my server (which is what the question asks) is not on Document root, only the content of the site.

  • @Bacco In certain cases like your Documentroot may not have been set, in addition to the mode presented by the bigown, maybe it will also work in these cases: echo substr($_SERVER['SCRIPT_FILENAME'], 0, -strlen($_SERVER['SCRIPT_NAME']));.

  • 2

    @Qmechanic73 I know what Documentroot is, Document root is the document folder (literal translation), which is where the pages of the site are. Even my system is modified to Document root point to virtual folders dynamically. Your answer is okay, what I posted was about what Gabriel said, as if your answer is the one that does what you were asked. But what was asked was not the folder of the site, but "folder of the server". This appears in Phpinfo, but it is not the one of your answer and neither of the bigown.

  • 3

    In other words: I believe that the author did not express himself well, so two good answers that do not answer what was asked literally - but answer the possible real need of the author (and if not, the author should express himself otherwise ;) )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.