Roy Granit
Roy Granit

Reputation: 335

Cannot exec R script in shell using php

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?

Upvotes: 0

Views: 424

Answers (2)

Roy Granit
Roy Granit

Reputation: 335

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

Upvotes: 1

digonly
digonly

Reputation: 11

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.

Upvotes: 0

Related Questions