What is the issue with using exec and shell_exec functions in PHP to run programs?

The issue with using exec and shell_exec functions in PHP to run programs is that it can pose a security risk if user input is not properly sanitized. This can lead to command injection attacks where malicious commands are executed on the server. To solve this issue, it is recommended to use escapeshellarg or escapeshellcmd functions to sanitize user input before passing it to exec or shell_exec.

$user_input = $_POST['user_input']; // Example user input

// Sanitize user input using escapeshellarg
$sanitized_input = escapeshellarg($user_input);

// Run the command safely
$output = shell_exec("ls " . $sanitized_input);
echo $output;