What are some potential pitfalls when using the exec() function in PHP?

One potential pitfall when using the exec() function in PHP is the risk of command injection if user input is not properly sanitized. To prevent this, always validate and sanitize user input before passing it to the exec() function. Additionally, be cautious of the commands being executed to avoid unintended consequences.

// Sanitize user input before passing it to exec()
$user_input = $_POST['input'];
$sanitized_input = escapeshellarg($user_input);

// Execute the sanitized command
exec("ls " . $sanitized_input);