What are the potential pitfalls of using exec in PHP for executing external commands?

One potential pitfall of using exec in PHP for executing external commands is the risk of command injection attacks if user input is not properly sanitized. To mitigate this risk, it is important to always validate and sanitize user input before passing it to the exec function.

// Example of properly sanitizing user input before using exec
$command = 'ls ' . escapeshellarg($user_input);
exec($command, $output);