I am trying to call a python script by php:
<?php
$a = exec("python ~/www/encrypt.py");
echo $a;
?>
I use terminal and php index.php command to see result and it works well but in a web browser it does not show anything.
Problem solved by full path >> exec("python /var/www/encrypt.py");
Related
I'm creating a webpage in PHP wherein if a button is pressed, the PHP script executes a python script using shell_exec() command. The python script creates a pdf file using fpdf python library. When I run the same from command shell, it works prefectly but when I try to run python script from PHP, it gives me nothing. Here is PHP code:
<!doctype html>
<?php
$command = escapeshellcmd('/home/amogh/server/test.py');
$output = shell_exec($command);
echo $output;
?>
Python script:
#! /usr/bin/python
#print ('hello')
from fpdf import FPDF
fp = open('downloads/boot.txt', 'r')
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', '', 11)
line = fp.read()
pdf.multi_cell(200, 5, line, 0, 1)
pdf.output('test.pdf', 'F')
Also, I'm using lighttpd server. Can anyone tell me where am I going wrong?
I'd guess the working directory is wrong and your script can't access the file it needs.
You may want to use
shell_exec("/home/amogh/server/test.py 2>&1");
in order to redirect stderr to stdout, so any error output from Python will be also in the $output variable, to help you debug things.
I've got my website hosted at one.com and I'd like to run a simple python script with php that returns 'hello' but I get no result.
It worked perfectly on local with Mamp.
What should I configure for it to work online ?
My php code:
<?php
$result = shell_exec('python test.py');
echo $result;
?>
The python script
print('hello')
First of all make sure that the python script is executable on the server. For safety reasons I would remove the space in the filename as well so make it just test.py for now. You can make it executable by simple changing the permissions using chmod +x test.py
Then, you can try the following lines of code in php
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
I am trying to use a PHP script to call a shell script and print the output to the browser.
I have confirmed that all permissions settings are set correctly, both the PHP and the bash script have full control.
If I run the shell script in the current directory (same directory as the PHP file) then it works fine. But if I try to run the shell script from /some/other/directory/shell.script, then it does not work. Why is this so?
I have tried to chdir() to the other directory, but after running a getcwd(), it never changed directories.
I have also tried the exec command by itself and it does not work.
<?php error_reporting(E_ALL); ini_set('display_errors',1);
echo "<h1>EDS Count Report</h1><p>";
$output=shell_exec('cd /app/script/catalog/ && ./EDScount_report.sh');
echo $output;
?>
contents of SHELL script:
#!/bin/bash
echo "this is a test!"
Try this please
<?php
$output=shell_exec('sh /path/to/otherdirectory/shellscript.sh');
echo $output;
?>
if you are certain that you can access that dicertory , you can use also this:
<?php
$output=shell_exec('cd /path/to/otherdirectory/ && ./shellscript.sh');
echo $output;
?>
I have a python script that uses ipyhon and it can be executed when I run it from the terminal using php -a (interactive shell), but when I type localserver/RDMIP_KE.php (test.php is the script wrtten with gedit in ubuntu) nothing happens and I only see a blank page. Any idea why?
<?php
$command = escapeshellcmd('/home/administrator/Desktop/RDMIP_KE.py');
$output = shell_exec($command);
echo $output;
?>
Just an update: I checked the log file and it gives me a list of errors for example about the pyplot: File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2460,ax = gca() in plot
But again as I said before the python program works fine and can be executed with php if I run it from the php shell. I don't understand what's going on.
I am using WAMP server on my system to execute php scripts.
I want to execute a script test.php from my main script main.php.
For that I am using exec function like this exec('php test.php');. In test.php I have given one echo statement.
But when I run my main.php script from the browser I am not able to see output of test.php script.
What am I doing wrong ? please suggest.
You have to give the proper path of php.exe
exec("c:\wamp\<where ever you exe is>/php.exe test.php");
so it has to be a proper path
use this command
echo exec('php test.php', $output); //this will print your echo statement.
print_r($output);