I want to run a PHP cli program from within PHP cli. On some machines where this will run, both php4 and php5 are installed. If I run the outer program as
Worth noting that now in PHP 5.4+ you can use the predefined constant - PHP_BINARY:
PHP_BINARY
Specifies the PHP binary path during script execution. Available since PHP 5.4.
Is there anything useful in $_ENV
?
The SHELL environment variable on Unix has the path to the shell that's currently running. If you add #!/path/to/php
to the top of your PHP file, make it executable and run the file directly, does $_ENV['SHELL']
contain /path/to/php
?
Okay, so this is ugly, but it works on Linux:
<?php
// Returns the full path of the current PHP executable
function get_proc_name(){
// Gets the PID of the current executable
$pid = posix_getpid();
// Returns the exact path to the PHP executable.
$exe = exec("readlink -f /proc/$pid/exe");
return $exe;
}
I'll try working on a Windows version later on.
EDIT
Doesn't look like there's any easy way to do this for Windows. Some Windows executables like tasklist
can give you the name of the executable, but not the full path to where the executable is. I was only able to find examples for finding the full path given a PID for C++, AutoHotkey and the like. If anyone has any suggestions on where else I could look, let me know.
PS: To get the PID in PHP for Windows, you apparently have to call getmypid().
You can try and parse the phpinfo() result.