3 ms·
Definitely not ideal, but php provides the escapeshellarg [1] function you are supposed to use in these situations. You can even implement a "safe" exec functi
by kilburn 7y ago
Definitely not ideal, but php provides the escapeshellarg [1] function you are supposed to use in these situations.
You can even implement a "safe" exec function like so:
function safe_exec(array $args, array &$output = null, int &$return_var = 0) : string {
return exec(
implode(' ',
array_map('escapeshellarg', $args)
), $output, $return_var
);
}
// Example usage
$output = [];
$return_var = 0;
echo safe_exec(['ls', '/tmp'], $output, $return_var);
var_export($output);
echo "Exit code: $return_var\n";
[1] https://www.php.net/manual/en/function.escapeshellarg.php https://www.php.net/manual/en/function.escapeshellarg.php