What are the advantages of using proc_open() over shell_exec for executing commands in PHP?

When executing commands in PHP, using proc_open() offers more control and flexibility compared to shell_exec(). With proc_open(), you can interact with the process input and output streams, set environment variables, and manage the process lifecycle more effectively. This can be useful for more complex command execution scenarios where you need more control over the process.

$descriptors = [
    0 => ['pipe', 'r'],  // stdin
    1 => ['pipe', 'w'],  // stdout
    2 => ['pipe', 'w']   // stderr
];

$process = proc_open('your_command_here', $descriptors, $pipes);

if (is_resource($process)) {
    fwrite($pipes[0], 'input_data_here');
    fclose($pipes[0]);

    $output = stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    $error = stream_get_contents($pipes[2]);
    fclose($pipes[2]);

    $return_value = proc_close($process);

    // Process $output, $error, and $return_value as needed
}