Eren Kara
Eren Kara

Reputation: 175

shell_exec does not execute from browser requests

After the user submits a data via POST, I show a temporary page and do the process on the background with shell_exec, atleast that's what I'm trying to do.

Here's my page setup:

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";
?>

If I go to localhost or localhost/index.php, the second script doesn't run. However, when I try to call both scripts from cmd, it works with both of them.

php C:\laragon\www\index.php

php C:\laragon\www\test.php

They both work and create a file called testfile.txt

Upvotes: 2

Views: 1958

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

Your webserver runs as a specific user and needs the path to php.exe as there is no path environment variable for the webserver user:

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

Upvotes: 5

Related Questions