Trent Trent - 1 year ago 124
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

Why is this?

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download