0

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?

3
  • 2
    Get rid of the double quotes around {$searched_image}, as escapeshellarg() is likely already adding single quotes for you. Build a string and feed that to shell_exec() so you can echo your command line and see what it actually is. Commented Feb 15, 2016 at 19:05
  • Still doesnt work when I remove double quotes. And when I build the string - $searched_image = escapeshellarg('./predict_altitude.sh -i /home/XXX/XXX/XXX/XXX/XXX/sp_dom1.jpg -p basic -o 0'); like this, it outputs exactly what I want to have in my command line - './predict_altitude.sh -i ....' and then when I call shell_exec($searched_image); it doesn't output result but it doesn't event output error.. Commented Feb 15, 2016 at 19:19
  • In your example in your comment, you're using escapeshellarg() incorrectly. You use it to escape a single argument, not a whole command line. Commented Feb 15, 2016 at 19:30

1 Answer 1

1

You write:

'./predict_altitude.sh -i "{$searched_image}" -p basic -o 0'

Inside single-quoted strings variables are not evaluated.

You can use this, instead:

"./predict_altitude.sh -i '{$searched_image}' -p basic -o 0"

Or - to avoid unpredictable evaluations - this:

$cmd = './predict_altitude.sh -i \''.$searched_image.'\' -p basic -o 0';
shell_exec( $cmd );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.