469,133 Members | 1,203 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

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
Jul 17 '05 #1
2 1447

James Pittman wrote:
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.


Have you tried $argv[0]

Ken

Jul 17 '05 #2
James Pittman napisa³:
Hi,
Hello
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?
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.php');
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.php".
I've tried all the $_SERVER[] variables to no
avail.
Nope, it won't work. These are only set in a webserver enviroment; forget
about them in a command-line parser.

Jamie


HTH
Mike
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Chris Martin | last post: by
6 posts views Thread by Lucas Cowald | last post: by
2 posts views Thread by Lucas Malor | last post: by
reply views Thread by BruceMcF | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.