Can you provide an example of a safer alternative to using shell_exec for executing shell commands in PHP?
Using shell_exec to execute shell commands in PHP can pose security risks, as it allows for arbitrary code execution. A safer alternative is to use the PHP function `proc_open`, which provides more control over the input and output streams of the command being executed.
$descriptorspec = [
0 => ['pipe', 'r'], // stdin
1 => ['pipe', 'w'], // stdout
2 => ['pipe', 'w'] // stderr
];
$process = proc_open('ls -l', $descriptorspec, $pipes);
if (is_resource($process)) {
// Read the output of the command
echo stream_get_contents($pipes[1]);
// Close the pipes
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
// Close the process
proc_close($process);
}