Get current PHP executable from within script?

前端 未结 10 1922
一个人的身影
一个人的身影 2020-12-03 21:08

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

         


        
相关标签:
10条回答
  • 2020-12-03 21:35

    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.

    0 讨论(0)
  • 2020-12-03 21:35

    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?

    0 讨论(0)
  • 2020-12-03 21:38

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

    0 讨论(0)
  • 2020-12-03 21:39

    You can try and parse the phpinfo() result.

    0 讨论(0)
提交回复
热议问题