What potential issues can arise when using shell_exec in PHP scripts?

When using shell_exec in PHP scripts, potential security vulnerabilities can arise if user input is directly passed to the command being executed. This can lead to command injection attacks where malicious commands are executed on the server. To mitigate this risk, it is important to sanitize and validate any user input before passing it to shell_exec.

// Sanitize and validate user input before passing it to shell_exec
$user_input = $_POST['input'];
$validated_input = escapeshellarg($user_input);

$output = shell_exec("ls " . $validated_input);
echo $output;