Jump to content

Finding the Document Root when executing PHP script on the command-line


jpalmer

Recommended Posts

Hello,

 

I'm wondering if anyone knows of a quick way to auto-discover the document root in a PHP script when the script is being executed via the command-line?

 

All the servers the script will be running on will be using Apache 1.x or Apache 2.x and the OS' will be either Windows, Redhat, OS X, or Solaris.

 

Any ideas?

 

The command getenv("DOCROOT") or $_SERVER['DOCUMENT_ROOT'] don't work when executing the script via the command-line.

If you are executing from the command-line then use this as a quick fix:

 

defined('__DIR__') or define('__DIR__', dirname(__FILE__));//PHP<5.3.0
chdir(__DIR__);

 

This will change the directory from your php executable to the directory in which your files reside.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.