shell_exec 不从浏览器请求执行

shell_exec does not execute from browser requests

用户通过 POST 提交数据后,我会显示一个临时页面并在后台使用 shell_exec 进行处理,至少这就是我正在尝试做的。

这是我的页面设置:

C:\laragon\www\index.php

<?php
    try {
        shell_exec("php " . __DIR__ . "/test.php");
    } catch(Exception $e) {
        echo "Exception: " . $e;
    }
?>

C:\laragon\www\test.php

<?php
    $myfile = fopen(__DIR__ . "/testfile.txt", "w");
    echo "test";
?>

如果我转到 localhost 或 localhost/index.php,第二个脚本不会 运行。但是,当我尝试从 cmd 调用这两个脚本时,它们都适用。

php C:\laragon\www\index.php

php C:\laragon\www\test.php

他们都工作并创建了一个名为 testfile.txt

的文件

您的网络服务器以特定用户身份运行,需要 php.exe 的路径,因为网络服务器用户没有路径环境变量:

shell_exec("c:\path\to\php " . __DIR__ . "/test.php");