Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.