CLI environment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    CLI environment

    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.o rders.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.p hp. its purpose is to
    abstract/simplify inclusionary script paths. it looks like:

    <?
    $parsedUri = dirname($_SERVE R['PHP_SELF']);
    $parsedUri .= substr($parsedU ri, -1) != '/' ? '/' : '';
    $relativeUri = str_replace('/', '', $parsedUri);
    $relativePath = strlen($parsedU ri) - strlen($relativ eUri) - 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.financia ls.cli.php):

    #!/usr/bin/php -q
    <?
    $cli = true;
    require_once 'relative.path. php';
    require_once $relativePath . 'site.cfg.php';
    require_once site::$rootDire ctory . 'admin/import.financia ls.php';
    ?>

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

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

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

    tia,

    me


  • purcaholic

    #2
    Re: CLI environment

    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.o rders.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.p hp. its purpose is to
    abstract/simplify inclusionary script paths. it looks like:
    >
    <?
    $parsedUri = dirname($_SERVE R['PHP_SELF']);
    $parsedUri .= substr($parsedU ri, -1) != '/' ? '/' : '';
    $relativeUri = str_replace('/', '', $parsedUri);
    $relativePath = strlen($parsedU ri) - strlen($relativ eUri) - 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.financia ls.cli.php):
    >
    #!/usr/bin/php -q
    <?
    $cli = true;
    require_once 'relative.path. php';
    require_once $relativePath . 'site.cfg.php';
    require_once site::$rootDire ctory . 'admin/import.financia ls.php';
    ?>
    >
    the site.cfg.php creates a singleton class 'site' and $rootDirectory would
    be the absolute path to the web root. import.financia ls.php begins like
    this:
    >
    <?
    if (!$cli)
    {
    $pageTitle = 'Import Financials';
    $fullHeader = false;
    $securityEnable d = true;
    require_once 'relative.path. php';
    require_once $relativePath . 'site.cfg.php';
    require_once site::$includeD irectory . 'head.inc.php'; }
    >
    ?>
    >
    however, as the error indicated, we never get to this point since
    import.financia ls.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


    Comment

    • ZeldorBlat

      #3
      Re: CLI environment

      On May 18, 1:54 pm, purcaholic <purcaho...@goo glemail.comwrot e:
      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.o rders.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.p hp. its purpose is to
      abstract/simplify inclusionary script paths. it looks like:
      >
      <?
      $parsedUri = dirname($_SERVE R['PHP_SELF']);
      $parsedUri .= substr($parsedU ri, -1) != '/' ? '/' : '';
      $relativeUri = str_replace('/', '', $parsedUri);
      $relativePath = strlen($parsedU ri) - strlen($relativ eUri) - 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.financia ls.cli.php):
      >
      #!/usr/bin/php -q
      <?
      $cli = true;
      require_once 'relative.path. php';
      require_once $relativePath . 'site.cfg.php';
      require_once site::$rootDire ctory . 'admin/import.financia ls.php';
      ?>
      >
      the site.cfg.php creates a singleton class 'site' and $rootDirectory would
      be the absolute path to the web root. import.financia ls.php begins like
      this:
      >
      <?
      if (!$cli)
      {
      $pageTitle = 'Import Financials';
      $fullHeader = false;
      $securityEnable d = true;
      require_once 'relative.path. php';
      require_once $relativePath . 'site.cfg.php';
      require_once site::$includeD irectory . 'head.inc.php'; }
      >
      ?>
      >
      however, as the error indicated, we never get to this point since
      import.financia ls.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($_SERVE R) from the CLI outputs all
      sorts of stuff -- including PHP_SELF.

      Comment

      • purcaholic

        #4
        Re: CLI environment

        On 18 Mai, 20:02, ZeldorBlat <zeldorb...@gma il.comwrote:
        On May 18, 1:54 pm, purcaholic <purcaho...@goo glemail.comwrot e:
        >
        >
        >
        >
        >
        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.o rders.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.p hp. its purpose is to
        abstract/simplify inclusionary script paths. it looks like:
        >
        <?
        $parsedUri = dirname($_SERVE R['PHP_SELF']);
        $parsedUri .= substr($parsedU ri, -1) != '/' ? '/' : '';
        $relativeUri = str_replace('/', '', $parsedUri);
        $relativePath = strlen($parsedU ri) - strlen($relativ eUri) - 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.financia ls.cli.php):
        >
        #!/usr/bin/php -q
        <?
        $cli = true;
        require_once 'relative.path. php';
        require_once $relativePath . 'site.cfg.php';
        require_once site::$rootDire ctory . 'admin/import.financia ls.php';
        ?>
        >
        the site.cfg.php creates a singleton class 'site' and $rootDirectory would
        be the absolute path to the web root. import.financia ls.php begins like
        this:
        >
        <?
        if (!$cli)
        {
        $pageTitle = 'Import Financials';
        $fullHeader = false;
        $securityEnable d = true;
        require_once 'relative.path. php';
        require_once $relativePath . 'site.cfg.php';
        require_once site::$includeD irectory . 'head.inc.php'; }
        >
        ?>
        >
        however, as the error indicated, we never get to this point since
        import.financia ls.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($_SERVE R) 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_R OOT"] 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.

        Comment

        • Schraalhans Keukenmeester

          #5
          Re: CLI environment

          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.o rders.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.

          Comment

          Working...