Get current PHP executable from within script?

前端 未结 10 1921
一个人的身影
一个人的身影 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:16

    You could use phpversion() to get the current version of PHP before you execute the "inner" script.

    http://php.net/manual/en/function.phpversion.php

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

    Unfortunately PHP_BINARY is returning the httpd binary (on windows XAMPP), so I'm back to using paths...

        if (defined('PHP_BINARY') && PHP_BINARY && in_array(PHP_SAPI, array('cli', 'cli-server')) && is_file(PHP_BINARY)) {
              return PHP_BINARY;
        } else if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            $paths = explode(PATH_SEPARATOR, getenv('PATH'));
            foreach ($paths as $path) {
                if (substr($path, strlen($path)-1)==DIRECTORY_SEPARATOR) {
                    $path=substr($path, 0, strlen($path)-1);
                }
                if ( substr($path, strlen($path) - strlen('php')) == 'php' ) {
                    $response=$path.DIRECTORY_SEPARATOR.'php.exe';
                    if (is_file($response)) {
                        return $response;
                    }
                } else if ( substr($path, strlen($path) - strlen('php.exe')) == 'php.exe' ) {
                    if (is_file($response)) {
                        return $response;
                    }
                }                
            }
        } else {
            $paths = explode(PATH_SEPARATOR, getenv('PATH'));
            foreach ($paths as $path) {
                if (substr($path, strlen($path)-1)==DIRECTORY_SEPARATOR) {
                    $path=substr($path, strlen($path)-1);
                }
                if ( substr($path, strlen($path) - strlen('php')) == 'php' ) {
                    if (is_file($path)) {
                        return $path;
                    }
                    $response=$path.DIRECTORY_SEPARATOR.'php';
                    if (is_file($response)) {
                        return $response;
                    }
                }               
            }
        }
        return null;
    
    0 讨论(0)
  • 2020-12-03 21:18

    This answer was helpful for tracking down a php location: How to get the path of the PHP BIN from PHP?

    It's also windows and linux friendly :)

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

    I know this question is a bit old, but it came up while looking for a shorter way of doing this.

    Unfortunately I can't find a shorter way, but this works pretty well and is multi OS/PHP version compatible.

    $lookIn could probably be extended to include more common locations.

    https://gist.github.com/cjsewell/39b60065ae2af677ceb1

    Hope it helps someone

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

    I think the bestconstant PHP_BINARY. With PHP 5.5.12

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

    On my server I've php 5.3.14.

    I've found a predefined constant: PHP_BIN_DIR

    Then, supposing the file name of the executable file is always 'php', $php_cmd = PHP_BIN_DIR.'/php' point to my PHP executable file.

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