My php code is in htdocs/series/index.php.
My python code location is /home/shivam/Desktop/hello.py
I am running php using xampp in ubuntu.
here is php code
<?php
$command = escapeshellcmd('/home/shivam/Desktop/hello.py');
$output = shell_exec($command);
echo $output;
?>
here is my python code
#!/usr/bin/env python
with open("/home/shivam/Desktop/solution.txt", "w") as myfile:
myfile.write("write text")
I am trying to run the python script using php to write to a file 'solution.txt'. When i write something myself to solution.txt and open it in read mode using python script via php then it works but when i try to write using above code ,i am unable to do so.
One more thing, none of the code below the line "with open("/home/shivam/Desktop/solution.txt", "w") as myfile:" gets executed.
I have tried the solution given in : enter link description here
Absolute paths to executables and files...
run_py.php
<?php
$command = escapeshellcmd('/usr/bin/python /home/user/some_py.py');
$output = shell_exec($command);
echo $output;
?>
some_py.py
with open("/home/user/solution.txt", "w") as myfile:
myfile.write("write text")
Output:
user#box:~/$ php run_py.php
user#box:~/$ cat solution.txt
write text
Related
Trying a very simple task. Call python example.py from a php script from a windows xampp server.
Notes:
- Both files are in the same directory.
- Going to http://example.com/example.py works fine.
- Going throught the command line (cd inside where example.py is then (python example.py)) works also.
example.py
#! C:{path-to-python.exe}
print("Content-Type: text/html\n")
print("<html>")
print("<h1>Something.</h1>")
index.php
<?php
$command = escapeshellcmd('python example.py');
$output = shell_exec($command);
echo $output;
?>
Nother note:
- If I use $command = escapeshellcmd('example.py'); it opens the python script in my text editor.
Someone tell me plz what am I doing wrong. Thank you.
to call python script from php :
$result=exec("python test.py ");
if you want to passing parameters to python script :
$result=exec("python test.py param1 param2");
in python script if you want get parameters:
import sys
param1=sys.argv[1]
param2=sys.argv[2]
note : $result in php taking first print statement in python script
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 using shell_exec function of PHP to execute a binary .exe file of a C program.
My C program takes an input text file and in output creates several text files. To integrate it with web front end, I am using php's shell_exec function to execute a shell script which in turn calls the binary C .exe file.
This binary .exe file , input text file ,shell script and the php file is on the server side. I have given chmod 777 access to the mentioned files on server side for testing purposes.
The extract in php executing shell script is as follows:
echo shell_exec('sh segments.sh 2>&1');
Now, here I am executing a shell script and the shell script runs the binary .exe file as follows:
->segments.sh
./auto_import.exe input_file.txt -terms
auto_import is the C binary .exe file.
When I run the shell script individually in my terminal, it gives me proper output. That is the output will be 4-5 different text files.
But when I call the shell script with PHP, I am not able to get any output and I am not sure how to receive output from my binary file which is number of text files.
I have tried providing absolute paths to my files for eg:
In php file shell script which is called by:
echo shell_exec('sh /path/segments.sh 2>&1');
In shell script , binary file is called as follows
echo shell_exec('sh /path/binary 2>&1');
I have also used exec function as follows:
$cmd = "/auto_import.exe input_file.txt -terms";
echo exec($cmd,$output);
Could you gives help me out in this regard?
If I understand you correctly, your command is executing as it should and producing files and you need to just read these now with PHP. One way of doing so is as follows:
<?php
$fileName = 'somefilethatyougenerated.txt';
$file = fopen($fileName, "r") or die("Unable to open file!");
$content = fread($file,filesize($fileName));
// manipulate contents here
fclose($myfile);
?>
If you need to read the file line by line, consider using "fgets", as shown here: How to read a file line by line in php
I have this Ruby script (test.rb):
print "hello"
And I have this PHP script (test.php):
$cmd = "ruby test.rb";
system($cmd);
Now I call my PHP script from CLI this way:
php test.php
And I get no output (it should print "hello")
Why?
system would capture the output of the ruby script.
you might want to do:
$cmd = "ruby test.rb";
echo system($cmd);
Its working for me, check whether both ruby script and php in the same folder and installed ruby in your machine
<?php
$cmd = "ruby test.rb";
system($cmd);
?>