php : Capturing the command output

前端 未结 3 654
借酒劲吻你
借酒劲吻你 2020-12-05 15:20

I am using exec function to execute the specific executable files in php .

exec ( $file , $output , $return_value  ) ;

When the given fil

相关标签:
3条回答
  • 2020-12-05 15:34

    The second argument $output only captures STDOUT from your executable. Error messages are usually sent to STDERR so that they easily can be written to an error log or similar, but this means that you won't see them when you call exec.

    If this is a linux system, you could append 2>&1 to your command, in order to redirect STDERR to STDOUT. I haven't tried this, but it should forward the error messages to your $output variable.

    Edit:

    I've read up on it on www.php.net/exec, and it seems this would work.

    exec($file.' 2>&1', $outputAndErrors, $return_value);
    

    It is also possible to redirect the errors to a temporary file and read them separately.

    exec($file.' 2> '.$tmpFile, $outputOnly, $return_value);
    

    Edit 2

    It seems windows also uses this Bourne style output redirecting syntax, so the examples should work for windows too.

    More on input and output streams

    0 讨论(0)
  • 2020-12-05 15:43

    The $return_value will have the error code returned by the program which should be meaningfull enough, I don't think you can have better.

    0 讨论(0)
  • 2020-12-05 15:47

    If you need to keep stderr and stdout separate, try proc_open: http://php.net/manual/en/function.proc-open.php

    0 讨论(0)
提交回复
热议问题