I'm trying to run shell_exec() with variable passed with AJAX from client.
This code causes error (input file doesn't exist!):
$searched_image = escapeshellarg("/home/XXX/XXX/XXX/XXX/XXX/sp_dom1.jpg");
$old_path = getcwd();
chdir('../elevation/source_code/altitudes_system/');
$altitudes_system_result = shell_exec('./predict_altitude.sh -i "{$searched_image}" -p basic -o 0');
chdir($old_path);
But when I replace "{$searched_image}" in shell_exec(...) with /home/XXX/XXX/XXX/XXX/XXX/sp_dom1.jpg code works well:
$old_path = getcwd();
chdir('../elevation/source_code/altitudes_system/');
$altitudes_system_result = shell_exec('./predict_altitude.sh -i /home/XXX/XXX/XXX/XXX/XXX/sp_dom1.jpg -p basic -o 0');
chdir($old_path);
Don't you have any idea why it works like this?
{$searched_image}, asescapeshellarg()is likely already adding single quotes for you. Build a string and feed that toshell_exec()so you can echo your command line and see what it actually is.escapeshellarg()incorrectly. You use it to escape a single argument, not a whole command line.