'Doubts about popen

Is there any way to make popen supports operators like &, | etc?

Example:

    $cmd = "/bin/sh -c" . " \"" . "whoami && uname" ."\"";
    $han = popen($cmd, 'r');
    echo(fread($han, 2096));
    pclose($han);

Just the first command ( whoami ) gets executed rather than both

I know there are other functions, but I'm talking just about popen

php


Solution 1:[1]

Use proc_open() with your command to ensure && will work:

proc_open() is similar to popen() but provides a much greater degree of control over the program execution.


<?php

    $cmd = "/bin/sh -c" . " \"" . "whoami && uname" ."\"";
    $han = proc_open($cmd, [ 0 => [ "pipe", "w" ]], $pipes);

    echo stream_get_contents($pipes[0]);
    proc_close($han);

Online demo


Output of above:

runner
Linux

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1