代码之家  ›  专栏  ›  技术社区  ›  Jakub Kulhan

Windows上的$\u服务器['\']等价物

  •  5
  • Jakub Kulhan  · 技术社区  · 15 年前

    $_SERVER["_"] 包含PHP解释器可执行文件的路径(例如 /usr/bin/php -r 'echo $_SERVER["_"];' 将打印 /usr/bin/php ). 在使用PHP5.3的Windows XP上 $\u服务器[“\u”] NULL

    5 回复  |  直到 15 年前
        1
  •  11
  •   Community Mohan Dere    6 年前

    这与PHP本身无关。是shell定义了环境变量。PHP只是把它捡起来

    here :

    shell设置了一些默认的shell变量;PS2就是其中之一。在Korn shell中设置或使用的其他有用shell变量有:

    • --当shell执行外部命令时,这将在新进程的环境中设置为已执行命令的路径。在交互式使用中,此参数也会在父shell中设置为上一个命令的最后一个单词。
    • ...

    PHP_FUNCTION(get_php_path)
    {
        char path[MAX_PATH];
        int result;
    
        if (zend_parse_parameters_none() == FAILURE)
            return;
        
        result = GetModuleFileNameA(NULL, path, MAX_PATH);
    
        if (result == 0)
            RETURN_FALSE;
    
        if (result == MAX_PATH) {
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Path is too large");
            RETURN_FALSE;
        }
    
        RETURN_STRINGL(path, result, 1);
    }
    

    例子:

    >php -r "echo get_php_path()";
    D:\Users\Cataphract\Documents\php-trunk\Debug_TS\php.exe
    
        2
  •  1
  •   2ndkauboy    15 年前

    找不到真正的解决办法php.exe文件,但您可以使用 include_path extension_dir 并转到它们的父目录php.exe文件应该保存。例如:

    echo str_replace('\ext', '', ini_get('extension_dir'));
    
        3
  •  0
  •   Pekka    15 年前

    我假设您指的是在CLI模式下运行PHP。

    我刚刚在Windows7上的CLIPPHP5.3中测试了这个,实际上似乎没有办法找到当前脚本运行的PHP可执行文件 $_SERVER["_"] 索引不可用;也没有包含可执行文件路径的任何其他变量)。

    Command Line PHP on Windows PHP手册中的页面对此问题没有任何说明。 argv php_sapi_name() 也不要透露任何事情。

    唯一想到的是在调用脚本之前设置一个环境变量的笨拙的解决方法:

    SET PHPEXE=C:\php\bin\php.exe
    c:\php\bin\php.exe -f scriptname.php
    

    然后在PHP脚本中:

    $php_exe_path = $_SERVER["PHPEXE"];
    

    不是很好,但我不知道。。。。

        4
  •  0
  •   Jamescun    15 年前

    $_SERVER['phprc'] . 'php.exe'

    会给你一些

    C:\程序文件\PHP\php.exe文件

        5
  •  0
  •   Oleg    8 年前

    我甩了你 $_SERVER 任何口味都有很多选择:)

    array(100) {
    
      ...
    
      ["PHPBIN"]=>
      string(43) "d:\openserver\modules\php\PHP-7-x64\php.exe"
      ["PHPDIR"]=>
      string(36) "d:\openserver\modules\php\PHP-7-x64\"
      ["PHPRC"]=>
      string(35) "d:\openserver\modules\php\PHP-7-x64"
      ["PHP_BIN"]=>
      string(43) "d:\openserver\modules\php\PHP-7-x64\php.exe"
      ["PHP_BINARY"]=>
      string(43) "d:\openserver\modules\php\PHP-7-x64\php.exe"
      ["PHP_BINDIR"]=>
      string(36) "d:\openserver\modules\php\PHP-7-x64\"
      ["PHP_DIR"]=>
      string(36) "d:\openserver\modules\php\PHP-7-x64\"
    
      ...
    
    }