In the context of PHP and Linux commands, what are some common pitfalls to avoid when using the "exec" function for executing external commands like "wget" and "mv"?

When using the "exec" function in PHP to execute external commands like "wget" and "mv" on a Linux system, it is important to be cautious of potential security risks such as command injection vulnerabilities. To mitigate this risk, always sanitize user input and validate the commands being executed to prevent unintended consequences.

// Sanitize user input and validate the command before executing
$user_input = $_POST['user_input']; // Example user input
$command = escapeshellcmd("wget $user_input"); // Sanitize user input
if (validate_command($command)) {
    exec($command);
} else {
    echo "Invalid command";
}

function validate_command($command) {
    // Implement command validation logic here
    return true; // Return true if command is valid, false otherwise
}