Getting script pathname in a script run from the command line

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

    Getting script pathname in a script run from the command line

    Hi,

    I have a PHP script running in a Cron job in Unix, using the "php"
    command-line command executable. How do I get the path to the script,
    from within the script? I've tried all the $_SERVER[] variables to no
    avail.

    Jamie
  • Ken Robinson

    #2
    Re: Getting script pathname in a script run from the command line


    James Pittman wrote:[color=blue]
    > Hi,
    >
    > I have a PHP script running in a Cron job in Unix, using the "php"
    > command-line command executable. How do I get the path to the[/color]
    script,[color=blue]
    > from within the script? I've tried all the $_SERVER[] variables to[/color]
    no[color=blue]
    > avail.[/color]

    Have you tried $argv[0]

    Ken

    Comment

    • Micha³ Wo¼niak

      #3
      Re: Getting script pathname in a script run from the command line

      James Pittman napisa³:
      [color=blue]
      > Hi,[/color]

      Hello
      [color=blue]
      > I have a PHP script running in a Cron job in Unix, using the "php"
      > command-line command executable. How do I get the path to the script,
      > from within the script?[/color]

      Do you mean the filename of the script, like
      "/foo/bar/whatever/script.php"?
      Then you should use the __FILE__ superconstant. It holds the filename of
      the currently executing script.
      BUT that means, that if you have:

      # in test_script.php :
      <?php
      echo 'First echo: ' . __FILE__ . '<BR>';
      ?>

      # in host_script.php :
      <?php
      include ('test_script.p hp');
      echo 'Second echo: ' . __FILE__;
      ?>

      you will get:
      First echo: (...)/test_script.php <BR>
      Second echo: (...)/host_script.php

      Chech the www.php.net page for more info on __FILE__ constant, you will
      get a lot of useful hints there. AFAIR they have something in the "See
      also:" section, that will give you always the "host_script.ph p".
      [color=blue]
      > I've tried all the $_SERVER[] variables to no
      > avail.[/color]

      Nope, it won't work. These are only set in a webserver enviroment; forget
      about them in a command-line parser.

      [color=blue]
      > Jamie[/color]

      HTH
      Mike

      Comment

      Working...