PHP proc_open Example  screenshot

PHP proc_open Example

Introduction

The PHP “proc_open” function allows us to execute an external application and communicate via two tunnels (the read pipe and write pipe).

If you want a complete description of proc_open function , please visit the official PHP webpage.

In this article we're going to see some examples of how implement this function.

Example: Get result of “ls -l” linux command

<?php
$descr = array(
    0 => array(
        'pipe',
        'r'
    ) ,
    1 => array(
        'pipe',
        'w'
    ) ,
    2 => array(
        'pipe',
        'w'
    )
);
$pipes = array();
$process = proc_open("ls -l", $descr, $pipes);
if (is_resource($process)) {
    while ($f = fgets($pipes[1])) {
        echo "-pipe 1--->";
        echo $f;
    }
    fclose($pipes[1]);
    while ($f = fgets($pipes[2])) {
        echo "-pipe 2--->";
        echo $f;
    }
    fclose($pipes[2]);
    proc_close($process);
}
?>

We use $pipe[1] array to know the output of the “ls” Linux command.

The same method could be used with other programs, commands and script bashes, for which we want to get the output result.

Example: communicating in-out with external applications

Another option is to interact with an external application, so we could send input data and get a final output result.

<?php
$descr = array(
    0 => array(
        'pipe',
        'r'
    ) ,
    1 => array(
        'pipe',
        'w'
    ) ,
    2 => array(
        'pipe',
        'w'
    )
);
$pipes = array();
$process = proc_open("php", $descr, $pipes);
if (is_resource($process)) {
    fputs($pipes[0], "");
    fclose($pipes[0]);
    while ($f = fgets($pipes[1])) {
        echo "-pipe 1--->";
        echo $f;
    }
    fclose($pipes[1]);
    while ($f = fgets($pipes[2])) {
        echo "-pipe 2--->";
        echo $f;
    }
    fclose($pipes[2]);
    proc_close($process);
}
?>

When we write in $pipes[0] array, we are sending input data to the application. Once we close the input channel, we can get the result of the external process, reading $pipes[1].

Example: waiting external application ends

<?php
$descr = array(
    0 => array(
        'pipe',
        'r'
    ) ,
    1 => array(
        'pipe',
        'w'
    ) ,
    2 => array(
        'pipe',
        'w'
    )
);
$pipes = array();
$process = proc_open("top -b -n 5", $descr, $pipes);
if (is_resource($process)) {
    while ($f = fgets($pipes[1])) {
        echo "-pipe 1--->";
        echo $f;
    }
    fclose($pipes[1]);
    while ($f = fgets($pipes[2])) {
        echo "-pipe 2--->";
        echo $f;
    }
    fclose($pipes[2]);
    proc_close($process);
}
?>

The function open_proc waits until the external application has finished to obtain the result. For example; Linux command “top -b -n 5” will do 5 iterations before ending. That is when “proc_open” can see the result.

Example: Get temperature of a city

We can use the PHP “proc_open” function to interact with external applications that show the temperature of any city in the world.

<?php
$descr = array(
    0 => array(
        'pipe',
        'r'
    ) ,
    1 => array(
        'pipe',
        'w'
    ) ,
    2 => array(
        'pipe',
        'w'
    )
);
$pipes = array();
$city = "http://www.accuweather.com/en/jp/tokyo/226396/weather-forecast/226396";
$commd = "xargs -0 curl -s  | awk -F\' '/acm_RecentLocationsCarousel\.push/{print $2\": \"$4\":  \"$16\", \"$12\"°\" }' | head -1";
$process = proc_open($commd, $descr, $pipes);
if (is_resource($process)) {
    fputs($pipes[0], $city);
    fclose($pipes[0]);
    while ($f = fgets($pipes[1])) {
        echo "-pipe 1--->";
        echo $f;
    }
    fclose($pipes[1]);
    while ($f = fgets($pipes[2])) {
        echo "-pipe 2--->";
        echo $f;
    }
    fclose($pipes[2]);
    proc_close($process);
}
?>

The same example could be implemented to verify if a file uploaded to website by a user is clean from viruses or not.

We could execute an antivirus program with path file of the uploaded file.