php exec: does not return output php exec: does not return output windows windows

php exec: does not return output


There are a few posts to the relevant sections of the PHP Manual such as this one:

I was having trouble using the PHP exec command to execute any batch file. Executing other commands (i.e., "dir") works fine). But if I executed a batch file, I receieved no output from the exec command.

The server setup I have consists of Windows Server 2003 server running IIS6 and PHP 5.2.3. On this server, I have:

  1. Granted execute permissions to the Internet User on c:\windows\system32\cmd.exe.
  2. Granted Everyone->Full Control to the directory in which the batch file is written.
  3. Granted Everyone->Full Control on the entire c:\cygwin\bin directory and its contents.
  4. Granted the Internet User "log on as batch" permissions.
  5. Specified the full path to each file being executed.
  6. Tested these scripts running from the command line on the server and they work just fine.
  7. Ensured that %systemroot%\system32 is in the system path.

It turns out that even with all of the above in place on the server, I had to specify the full path to cmd.exe in the exec call.

When I used the call: $output = exec("c:\\windows\\system32\\cmd.exe/c $batchFileToRun");

then everything worked fine. In my situation, $batchFileToRun was the actual system path to the batch file (i.e., the result of a call to realpath()).

There are a few more on both the exec and shell_exec manual pages. Perhaps following through them will get it up and working for you.


The main reason you get zero output from a command such as dir is because dir doesn't exist. It's part of the command prompt, and the solution to this particular problem is well, in this page somewhere.

You can find this out by pressing WIN + R, and typing in dir or start for that matter - an error message appears!

However perverse this may sound, I've found that the most reliable way to do anything process related in Windows is with using the Component Object Model. You said for us to share our experiences, right?

$me hears people laughing

Regained your composure yet?

So, first we'll create the COM object:

$pCOM = new COM("WScript.Shell");

Then, we'll just run what ever needs to be run.

$pShell = $pCom->Exec("C:\Random Folder\Whatever.exe");

Cool! Now, that'll hang until everything's finished in that binary. So, what we need to do now is grab the output.

$sStdOut = $pShell->StdOut->ReadAll;    # Standard output$sStdErr = $pShell->StdErr->ReadAll;    # Error

There's some other things you can do - find out what was the process ID, error code, etc. Although this example is for Visual Basic, it's based on the same scheme.


EDIT: After a few researchs, the only way i see to execute the DIR command is like that:

cmd.exe /c dir c:\

So you can try my solution using:

$command = 'cmd.exe /c dir c:\\';

You can use proc_open function too, which gives you access to stderr, return status code and stdout.

    $stdout = $stderr = $status = null;    $descriptorspec = array(       1 => array('pipe', 'w'),  // stdout is a pipe that the child will write to       2 => array('pipe', 'w') // stderr is a pipe that the child will write to    );    $process = proc_open($command, $descriptorspec, $pipes);    if (is_resource($process)) {        // $pipes now looks like this:        // 0 => writeable handle connected to child stdin        // 1 => readable handle connected to child stdout        // 2 => readable handle connected to child stderr        $stdout = stream_get_contents($pipes[1]);        fclose($pipes[1]);        $stderr = stream_get_contents($pipes[2]);        fclose($pipes[2]);        // It is important that you close any pipes before calling        // proc_close in order to avoid a deadlock        $status = proc_close($process);    }    return array($status, $stdout, $stderr);

The interesting part with proc_open, compared to other functions like popen or exec, is that it provides options specific to windows platform like:

suppress_errors (windows only): suppresses errors generated by this function when it's set to TRUEbypass_shell (windows only): bypass cmd.exe shell when set to TRUE