【问题标题】:echo shell_exec("echo %USERPROFILE%") does not return the right valueecho shell_exec("echo %USERPROFILE%") 没有返回正确的值
【发布时间】:2014-05-14 21:45:02
【问题描述】:

我正在尝试从 php shell_exec 和 echo %USERPROFILE% 获取 Windows 上的用户配置文件路径。

dos 命令本身可以正常工作:

C:\>echo %USERPROFILE%
C:\Users\username

但从 php 只返回 '%USERPROFILE%':

echo shell_exec("echo %USERPROFILE%");

我也试过 getenv('USERPROFILE');但即使将 phi.ini variables_order 更改为“EGPCS”,我也没有得到任何返回

【问题讨论】:

    标签: php environment-variables shell-exec


    【解决方案1】:
    C:\Users\temp_000>php -r "var_dump(getenv('USERPROFILE'));"
    string(17) "C:\Users\temp_000"
    
    C:\Users\temp_000>php -r "var_dump($_ENV['USERPROFILE']);"
    string(17) "C:\Users\temp_000"
    
    C:\Users\temp_000>php
    <?php
    var_dump(shell_exec("echo %USERPROFILE%"));
    ^Z
    string(18) "C:\Users\temp_000
    "
    

    一切都为我工作。我能想到的唯一一件事是你在 Linux 上运行你的 PHP 脚本,但你说你在 Windows 上,所以......不太确定。反正就是想展示一下我的测试结果。

    【讨论】:

    • 谢谢,是的,不知道是什么问题,也许我有其他一些 php.ini 配置。
    猜你喜欢
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2013-06-06
    • 1970-01-01
    相关资源
    最近更新 更多