CLI environment

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
i'm trying to re-use scripts, typically called via a browser, in the php  
cli. the cli calls are made through cron jobs. i'm hitting some unknowns as  
to path info. here's the error i'm getting thus far:

"PHP Warning:  require_once(../../../../../../site.cfg.php): failed to open  
stream: No such file or directory in  
/var/www/html/dev/admin/cli/ on line 5"

i'm executing the cli from:

/home/me/www/dev/admin (www is a symlink to /var/www/html/)

first, what is the current directory for php cli when executed from a cron?  
second, will i need to adjust the current directory or anything else so that  
other included/required files (normally accessed via a browser) are  
referenced correctly?

thanks for your input.


here's the pertanent code...

my include path is:


in that directory i have a file named relative.path.php. its purpose is to  
abstract/simplify inclusionary script paths. it looks like:

$parsedUri        = dirname($_SERVER['PHP_SELF']);
$parsedUri       .= substr($parsedUri, -1) != '/' ? '/' : '';
$relativeUri      = str_replace('/', '', $parsedUri);
$relativePath     = strlen($parsedUri) - strlen($relativeUri) - 1;
if ($relativePath < 0){ $relativePath = 0; }
$relativePath     = str_repeat('../', $relativePath);
if (!$relativePath){ $relativePath = './'; }

i'm wrapping the files i'm trying to re-use via cli in code similar to (call  
it, import.financials.cli.php):

#!/usr/bin/php -q
$cli = true;
require_once 'relative.path.php';
require_once $relativePath        . 'site.cfg.php';
require_once site::$rootDirectory . 'admin/import.financials.php';

the site.cfg.php creates a singleton class 'site' and $rootDirectory would  
be the absolute path to the web root. import.financials.php begins like  

if (!$cli)
  $pageTitle        = 'Import Financials';
  $fullHeader       = false;
  $securityEnabled  = true;
  require_once 'relative.path.php';
  require_once $relativePath           . 'site.cfg.php';
  require_once site::$includeDirectory . '';

however, as the error indicated, we never get to this point since  
import.financials.cli.php cannot find site.cfg.php (as it normally would if  
called from a browser).



Re: CLI environment

Quoted text here. Click to load it

Hi Steve,

the superglobal $_SERVER exists only, if PHP is running as a module or
cgi (webserver). Use __FILE__ instead of $_SERVER['PHP_SELF'].
Or you can put a workaround in your cli wrapper, by setting
$_SERVER['PHP_SELF'] or other needed $_SERVER[*] variables.


Re: CLI environment

Quoted text here. Click to load it

I beg to differ.  Running print_r($_SERVER) from the CLI outputs all
sorts of stuff -- including PHP_SELF.

Re: CLI environment

Quoted text here. Click to load it

You're right, i should think a while before posting such a wrong
answer. Have confused it with $_SERVER["DOCUMENT_ROOT"] which isn't
available from cli call.

Forgott what i wrote, you're problem is not $_SERVER variable. Maybe
setting current working directory to your root directory (/var/www/
html/) could be a sollution.

Re: CLI environment

At Fri, 18 May 2007 11:51:24 -0500, Steve let his monkeys type:

Quoted text here. Click to load it

Not surprising PHP can't find it seeing the file is searched 6 dir
levels up (probably your system root I'd say)

Perhaps you'd better opt for full paths instead of relative, in that case
it doesn't matter what the cwd is during execution of your script.

Opinions differ however on what's best practice, relative or absolute.

Site Timeline