I’m trying to use the PHP passthru function to execute an OS command on Linux.
I am using it as follows. The Linux command is just to list files in a directory I know does not exist and then I am just echoing the status to make sure it’s not 0.
JavaScript
x
$osexec = "ls /tp";
$status = 0;
$result = passthru($osexec, $status);
echo $status;
However the status seems to be the entire output of the command including the status number
JavaScript
ls: /tp: No such file or directory
2
Why is this?
Advertisement
Answer
You are seeing stderr
plus the $status
value. To hide stderr
you could do the following:
JavaScript
// 2> /dev/null to hide stderr
$osexec = "ls /tp 2> /dev/null";
$status = 0;
$result = passthru($osexec, $status);
echo $status;