0

I'm trying to build an interface which allows entering a query that is to be passed to an R script (found in the same folder) which does some calculation and prints the output:

<?php

$author=$_POST['data'];

echo $author."<br>";

$output = shell_exec("Rscript --vanilla h-index.R '$author' ");

echo "<pre>$output</pre>";


?>

Yet for some reason the script won't fire.. I gave it exec permissions and tested it via the command line and it works.

Any ideas?

2
  • 1
    Have you tried using the full path to your h-index.R script? It's possible that shell_exec() does not keep the same working directory as your PHP script. Also, shell_exec() only returns stdout. If Rscript gives an error on stderr, you could try adding 2>&1 at the end of the command to redirect that error to stdout so that you can see it. Commented Jul 22, 2018 at 11:38
  • Yes, I tried with the full path.. and also made sure they are on the same folder (by exec pwd..). Thanks for the tip about 2>&1 ! it says sh: Rscript: command not found Commented Jul 22, 2018 at 14:02

2 Answers 2

1

OK, I figured out thanks to rickdenhaan that shell did not recognise the 'Rscript' command and using the absolute path /usr/local/bin/Rscript it worked

Sign up to request clarification or add additional context in comments.

Comments

0

Most likely the process running php has in its .ini file the directive disabled_functions="shell_exec" and perhaps also others.

This is done for security reasons. Check with your hosting provider whether this is the case. Note that they most likely won't be willing to change this setting though.

The command line version of php has a different .ini file than the webserver or process manager for php. This is most likely the reason you could use shell_exec when invoking from the command line.

As a side note: You should perform at least some sanitisation on the argument you accept in the $_POST variable.

1 Comment

I can actually exec via sell certain commands such as pwd, ls, grep etc.. without any trouble

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.