In what scenarios might functions like exec, shell_exec, and system be deactivated or restricted in PHP, and how can developers work around these limitations?

Functions like exec, shell_exec, and system may be deactivated or restricted in PHP for security reasons on shared hosting environments or in certain server configurations. To work around these limitations, developers can use alternative methods such as using the proc_open function to execute external commands in a more controlled manner.

// Example workaround using proc_open
$command = 'ls -la';
$descriptors = [
    0 => ['pipe', 'r'], // stdin
    1 => ['pipe', 'w'], // stdout
    2 => ['pipe', 'w'], // stderr
];

$process = proc_open($command, $descriptors, $pipes);

if (is_resource($process)) {
    // Read output from the command
    $output = stream_get_contents($pipes[1]);
    
    // Close the pipes and the process
    foreach ($pipes as $pipe) {
        fclose($pipe);
    }
    proc_close($process);

    echo $output;
}