What are some potential pitfalls of using the exec() function in PHP to run external commands like unrar?
One potential pitfall of using the exec() function in PHP to run external commands like unrar is the risk of command injection attacks if user input is not properly sanitized. To mitigate this risk, it is important to validate and sanitize any user input before passing it to the exec() function. This can help prevent malicious commands from being executed on the server.
$input = $_POST['input']; // Assuming input is coming from a form field
// Validate and sanitize user input
if (preg_match('/^[a-zA-Z0-9\-\_\. ]+$/', $input)) {
// Run the unrar command using exec()
$output = exec("unrar x " . escapeshellarg($input));
echo "Command output: " . $output;
} else {
echo "Invalid input";
}