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? Link to comment https://forums.phpfreaks.com/topic/162624-solved-document_root/ 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 Link to comment https://forums.phpfreaks.com/topic/162624-solved-document_root/#findComment-858292 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. Link to comment https://forums.phpfreaks.com/topic/162624-solved-document_root/#findComment-858311 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; ?> Link to comment https://forums.phpfreaks.com/topic/162624-solved-document_root/#findComment-858337 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, Link to comment https://forums.phpfreaks.com/topic/162624-solved-document_root/#findComment-858391 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'])); Link to comment https://forums.phpfreaks.com/topic/162624-solved-document_root/#findComment-858633 Share on other sites More sharing options...
jaymc Posted June 18, 2009 Author Share Posted June 18, 2009 Cheers I've sorted it Link to comment https://forums.phpfreaks.com/topic/162624-solved-document_root/#findComment-859056 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.