By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

CLI environment

P: n/a
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/import.repair.orders.cli.php 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:

/usr/share/pear

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
this:

<?
if (!$cli)
{
$pageTitle = 'Import Financials';
$fullHeader = false;
$securityEnabled = true;
require_once 'relative.path.php';
require_once $relativePath . 'site.cfg.php';
require_once site::$includeDirectory . 'head.inc.php';
}
?>

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).

tia,

me
May 18 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 18 Mai, 18:51, "Steve" <no....@example.comwrote:
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/import.repair.orders.cli.php 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:

/usr/share/pear

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
this:

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

?>

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).

tia,

me
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.

purcaholic
May 18 '07 #2

P: n/a
On May 18, 1:54 pm, purcaholic <purcaho...@googlemail.comwrote:
On 18 Mai, 18:51, "Steve" <no....@example.comwrote:
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/import.repair.orders.cli.php 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:
/usr/share/pear
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
this:
<?
if (!$cli)
{
$pageTitle = 'Import Financials';
$fullHeader = false;
$securityEnabled = true;
require_once 'relative.path.php';
require_once $relativePath . 'site.cfg.php';
require_once site::$includeDirectory . 'head.inc.php';}
?>
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).
tia,
me

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.

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

May 18 '07 #3

P: n/a
On 18 Mai, 20:02, ZeldorBlat <zeldorb...@gmail.comwrote:
On May 18, 1:54 pm, purcaholic <purcaho...@googlemail.comwrote:


On 18 Mai, 18:51, "Steve" <no....@example.comwrote:
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/import.repair.orders.cli.php 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:
/usr/share/pear
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
this:
<?
if (!$cli)
{
$pageTitle = 'Import Financials';
$fullHeader = false;
$securityEnabled = true;
require_once 'relative.path.php';
require_once $relativePath . 'site.cfg.php';
require_once site::$includeDirectory . 'head.inc.php';}
?>
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).
tia,
me
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.
purcaholic

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

- Zitierten Text anzeigen -
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.

@steve:
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.

May 18 '07 #4

P: n/a
At Fri, 18 May 2007 11:51:24 -0500, Steve let his monkeys type:
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/import.repair.orders.cli.php on line 5"
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.
Sh.

May 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.