-1

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?

FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
Ryder389
  • 43
  • 1
  • 1
  • 12
  • Please don't change the question to feature a new error after the original question was solved. It invalidates the answers already given when you incorporate them into your question. Ask separate questions for seperate problems. – FlyingTeller Oct 09 '18 at 11:22

1 Answers1

1

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.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Thanks for the update.. After making the changes, I run the script again and got this error `Traceback (most recent call last): File "/home/amogh/server/test.py", line 3, in from fpdf import FPDF ImportError: No module named fpdf `. I don't understand, When I run the same script from command shell, it works perfectly and when I run from the PHP script, it gives me this error. – Ryder389 Oct 09 '18 at 06:26
  • How have you installed `fpdf`? How do you run the command on the command line? (Please update these details in the main question.) – AKX Oct 09 '18 at 06:27
  • @Ryder389 please accept this answer as it solved the original problem. You have already made a separate question for this new problem – FlyingTeller Oct 09 '18 at 11:20