jpalmer Posted August 28, 2009 Share Posted August 28, 2009 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. Link to comment https://forums.phpfreaks.com/topic/172276-finding-the-document-root-when-executing-php-script-on-the-command-line/ Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 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. Link to comment https://forums.phpfreaks.com/topic/172276-finding-the-document-root-when-executing-php-script-on-the-command-line/#findComment-908323 Share on other sites More sharing options...
jpalmer Posted August 28, 2009 Author Share Posted August 28, 2009 This is a great fix! Thank you! Link to comment https://forums.phpfreaks.com/topic/172276-finding-the-document-root-when-executing-php-script-on-the-command-line/#findComment-908325 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.