Trent Trent - 11 days ago 6
Linux Question

PHP passthru execution status

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.

$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

ls: /tp: No such file or directory
2


Why is this?

Answer

You are seeing stderr plus the $status value. To hide stderr you could do the following:

// 2> /dev/null to hide stderr
$osexec = "ls /tp 2> /dev/null";
$status = 0;

$result = passthru($osexec, $status);

echo $status;