jaymc Posted June 17, 2009 Share Posted June 17, 2009 I use $_SERVER['DOCUMENT_ROOT'] to check the path of a script however I have just realised if you run a script outside of apache, $_SERVER variable does not contain anything So my question is, when running php script via cronjob or command line how can I retreive the document root of the file? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 17, 2009 Share Posted June 17, 2009 well...what are you trying to accomplish exactly...you have a couple options: getcwd() - Gets the current working directory dirname(__FILE__) - Gets the directory the current file is in edit: __FILE__ is a global variable by the way, not a typo Quote Link to comment Share on other sites More sharing options...
jaymc Posted June 17, 2009 Author Share Posted June 17, 2009 dirname(__FILE__) is what I was looking for Thanks. Quote Link to comment Share on other sites More sharing options...
jaymc Posted June 17, 2009 Author Share Posted June 17, 2009 Wait sorry it doesn't do what I need <? // this php file exists in /home/jay/live/www/scripts/file.php // I want $dir to = /home/jay/live/www/scripts/ // not /home/jay/ include("/home/jay/global.php"); // this has $dir = dirname(__FILE__); echo $dir; ?> Quote Link to comment Share on other sites More sharing options...
jaymc Posted June 17, 2009 Author Share Posted June 17, 2009 From php.net. highlighted is the problem __FILE__ : The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 18, 2009 Share Posted June 18, 2009 There is no good way to determine a DOCUMENT_ROOT in this situation. Your global.php is outside of your site's directory. You could always do this: $dir = dirname(__FILE__).'/live/www/scripts/'; or how about: $dir = dirname(realpath($_SERVER['SCRIPT_NAME'])); Quote Link to comment Share on other sites More sharing options...
jaymc Posted June 18, 2009 Author Share Posted June 18, 2009 Cheers I've sorted it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.