What potential issues could arise when using the system() function in PHP for executing system commands?

One potential issue when using the system() function in PHP is the risk of command injection attacks if user input is not properly sanitized. To mitigate this risk, always validate and sanitize user input before passing it to the system() function.

// Example of validating and sanitizing user input before using system() function
$user_input = $_POST['input'];

// Validate and sanitize user input
if (preg_match('/^[a-zA-Z0-9\s]+$/', $user_input)) {
    // Safe to execute system command
    $output = system("ls " . escapeshellarg($user_input));
} else {
    echo "Invalid input.";
}