I am trying to execute R script from Php. It works fine with the command prompt with this :
Documents\R\R-3.5.0\bin\R.exe Desktop\my_script.R
But the same is not working from PHP call using exec. Any suggestion please!
<?php
exec("Documents\R\R-3.5.0\bin\R.exe Desktop\my_script.R", $output);
print_r($output);
?>
print_r($output) displays only "Array()" no correct result ;
Those paths must be relative to the php working directory. Otherwise they must be fully specified:
Try:
$cmd = "C:\Users\YOURNAME\Documents\R\R-3.5.0\bin\Rscript.exe C:\Users\YOURNAME\Desktop\my_script.R";
exec($cmd, $output);
Related
I need to run a python program with command line argument from my php script
This is my code
<?php
$output=shell_exec('python /path/to/python_from_php.py input1 input2');
echo $output;
?>
I am getting the contents inside shell_exec function as the output.
I tried using only
$output=exec('python /path/to/python_from_php.py input1 input2');
I am getting only the second input as my output.
But what I want is I should be able to print the input contents through my python script. If I remove the "echo $output" line I am not getting any output.
My python code
import sys
a=(sys.argv)
for i in (sys.argv):
print i
this code gives you the output from shell
<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>
from this reference php also ensure php is not running in safe mode
but you could try just call the python script from php
<?php
$my_command = escapeshellcmd('python path/to/python_file.py');
$command_output = shell_exec($my_command);
echo $command_output;
?>
As OP wants to:
But what I want is I should be able to print the input contents
through my python script. If I remove the "echo $output" line I am not
getting any output.
should be used passthru, description
<?php
passthru('python /path/to/python_from_php.py input1 input2');
?>
Check, that you php user have access to run python and your python script.
Live demo is here
One option is to use passthru(), but if you don't want to manage the output buffer, and having the output in an array is desirable, you can add an output parameter to the exec command as so:
<?php
error_reporting(E_ALL);
$command = escapeshellcmd('/usr/bin/python /path/to/python_from_php.py arg1 arg2');
exec($command, $output);
for ($l=0; $l < count($output); ++$l) {
echo $output[$l]."\n";
};
I'm currently creating a php page which does a ssh call with a .rb (ruby) file.
rb file
require 'metainspector'
page = MetaInspector.new("www.hln.be")
puts page.image
When creating a php file with the following code (php):
$cmd = "ruby facescrape.rb";
$last_line = system($cmd, $retval);
echo $last_line . '
echo $retval;
this only returns value 1.
However 2 things :
When running the same command in ssh, it will print the page.image
correctly.
When i change the rb file and for instance set as last line
puts "test"
this value returns correctly with also print correctly with the aboven php code.
I don't get why printing the page.image works in ssh but won't work by using that php code.
Also tried using exec() instead of system().
Thank you in advance!
Kind regards,
Kurt Colemonts
I used following command to convert the 3d models with the assimp Assimp, and it is working fine on Windows:
assimp2json seaknight.obj seaknight.json
I need to know how can I run this command from the PHP? I know that there re functions to run the shell execution from PHP, but it didn't work and I don't get any error.
PHP code is used is follows.
system("D:\assimp2json-2.0-win32\Release\assimp2json.exe assimp2json seaknight.obj seaknight.json");
and another one is
$old_path = getcwd();
chdir('D:\assimp2json-2.0-win32\Release');
$output = shell_exec('assimp2json.exe assimp2json seaknight.obj seaknight.json');
chdir($old_path);
Found it myself
working code is below
$old_path = getcwd();
chdir('D:\assimp2json-2.0-win32\Release');
$output = shell_exec('assimp2json monster.blend monster.json');
chdir($old_path);
no need to include the .exe, after remove it the command worked
Hi i m trying to execute a bash file which contains a command to run a script file. I had been trying all possible methods but nothing seem to work. I want to run the file and display the result on the web page.
the bash file contains arguments to execute the script. i tried with
system(./bashfile path);
shell_exec(./file path);
if(isset($_POST['submit'])) {
$output = shell_exec('./Users/file.sh ');
echo "<pre>$output</pre>";
but nothing happens. what other ways i could execute the .sh file, help appriciated
system(),exec(),shell_exec(),... functions might be disabled because of security.
Also on the phpinfo page of shell_exec you can find:
This function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required.
try:
$command = './Users/file.sh';
$output = '';
exec ( $command, $output, $return_var );
var_dump($output);
var_dump($return_var);
Trying to figure this out. I am trying to execute a perl script within php, using shell_exec() like so:
<?php
$output=shell_exec("./tst.pl > test.txt");
//$output=shell_exec("./tst.pl");
echo $output;
?>
It will not write output to a file using ">" filename.txt.
It will work if I execute without directing it to a filename as I can confirm this with echo.
Does this have to do with using ">"?
Permissions should be fine as I am able to run the same perl script on command line and direct to file. Any suggestions for executing this?
The output of "test.txt" will be used as input:
<?php
$data = array();
$InputFile = file("test.txt");
...
?>
It was definitely a permissions problem. Wrote the file out to /tmp and it worked fine.
<?php
$output=shell_exec("./tst.pl > /tmp/test.txt");
echo $output;
?>