shmax Posted February 28, 2009 Share Posted February 28, 2009 I used to use $_SERVER['DOCUMENT_ROOT'] all over the place, particularly for doing includes. I then found out about php5's autoload feature, which led me to discover the include_path in php.ini. That enabled me to stop using DOCUMENT_ROOT for my includes--I would just include them with no root path attached. So far, so good. The tricky bit is cron jobs. Scripts run in the cron environment on my server don't have access to the usual $_SERVER variables, nor do they reference the same php.ini file. I eventually discovered that you can specify a php.ini file using the -c paramater on the command line when calling your cron job. That got me access to the include_path variable, so all of my includes now work. The last remaining unsolved mystery is how to handle file i/o--it seems that fopen and other i/o functions don't examine the include paths when trying to open a file. Many of my cron scripts need to write to logs that I've set up. When I use these same logging functions in my non-cron scripts, I use the $_SERVER['DOCUMENT_ROOT'] to specify the location of a log file, but in the cron scripts I'm stuck. For a while I satisifed myself with a crude hack, where I simply manually set $_SERVER['DOCUMENT_ROOT'] to what I knew was the absolute path to the home directory, but then I can't run the same scripts on my local server (which has a different directory structure). I found a snippet of code somewhere that attempts to derive the root directory from information drawn from the current running script: function GetDocRoot() { $temp = getenv("SCRIPT_NAME"); $localpath=realpath(basename(getenv("SCRIPT_NAME"))); $localpath=str_replace("\\","/",$localpath); $docroot=substr($localpath,0, strpos($localpath,$temp)); return $docroot; } Works great in non-cron scripts (where I don't need it), but fails in the cron environment. Blocked at every turn. Surely this problem hits everyone who works with cron jobs? Why can't I find a solid, "universal" solution to this? Is there some obvious fix that everyone but me knows about? Is this something that can be solved by fiddling with php.ini? I know there's a doc_root variable in there, but when I set it and check phpinfo, my changes don't "take". Any ideas? Thanks! Max Quote Link to comment https://forums.phpfreaks.com/topic/147299-solved-universal-absolute-path-to-document-root-solution/ Share on other sites More sharing options...
MadTechie Posted February 28, 2009 Share Posted February 28, 2009 The trick i use is to find where the current scipt is first So for this setup -config.php +-class +-myclass.php <?php $root = dirname(__FILE__); //I am here! ?> <?php $here = dirname(__FILE__); include $here."/../config.php"; //back one from here and include config.php echo "root = $root<br>"; echo "here= $here<br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/147299-solved-universal-absolute-path-to-document-root-solution/#findComment-773216 Share on other sites More sharing options...
cooldude832 Posted February 28, 2009 Share Posted February 28, 2009 Just an idea, but you can take phpinfo(); and use regex to actually disassemble the output read what the include_path(); is in the php.ini and then store it in a flat file that is in the root. Use a CRON job to update the CRON job path finder every so often if you want to ge really facy. Just an idea Quote Link to comment https://forums.phpfreaks.com/topic/147299-solved-universal-absolute-path-to-document-root-solution/#findComment-773222 Share on other sites More sharing options...
MadTechie Posted February 28, 2009 Share Posted February 28, 2009 if you wanted to just get the includes then instead of paraing the phpinfo.. just get it directly use ini_get('include_path') Quote Link to comment https://forums.phpfreaks.com/topic/147299-solved-universal-absolute-path-to-document-root-solution/#findComment-773225 Share on other sites More sharing options...
shmax Posted March 1, 2009 Author Share Posted March 1, 2009 Thanks, guys, some pretty creative solutions, though all of them were probably more convoluted/brittle than my cheap method of manually setting the $_SERVER['DOCUMENT_ROOT']. I finally discovered that I was mistaken about doc_root--it is being set properly, and can be easily retrieved using get_ini (as long as you have provided a php.ini file on the command line). So, problem solved. The code is portable, works in cron and non-cron settings, doesn't require the client code to consider its own relative location in the directory structure, nor does it rely on brute-force include path parsing: function WriteLog( $str, $logFile = "profile.log" ) { $docRoot = ini_get( 'doc_root' ); $logFilename = $docRoot."/logs/$logFile"; $fp = fopen( $logFilename, "a+"); fwrite($fp, $str."\r\n"); fclose($fp); } Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/147299-solved-universal-absolute-path-to-document-root-solution/#findComment-773646 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.