0

I have a php file in my XAMPP-VM server which looks like this:

<?php

$message = exec('python testPython.py');
echo($message);

 ?>

the testPython.py file looks like this:

f = open('data.txt', 'w+')
f.write('test')
print('hello')
f.close()

When I run the php file like so localhost/testPHP.php, the testPython.py file gets stuck on line one and does not move on. When I commented out the first, second, and last line of code, the program worked fine and printed out "hello." Interestingly, when I run the testPython.py file from terminal like this python testPython.py it works. It opens data.txt and writes "test." Does anyone know how I could fix this problem?

note: I know that I could easily do the same thing from php without executing a python file at all but this post is just showcasing my problem. I need to open a file in a python file that I have already put a lot of work into.

Max Litvak
  • 115
  • 6
  • 2
    Perhaps the working directory is not what you expect in the case of running from PHP. – wallyk Feb 26 '19 at 03:07
  • If stderr reveals nothing, the python interpreter itself offers verbose and debugging output. – mario Feb 26 '19 at 03:39

1 Answers1

0

first you can make a file call it runpy and put this on it

python testPython.py

and then in ur php run

exec('bash runpy');

second make sure the folder is set to 777, because when you run a cmd using exec you run it as the apache user www-data, while in the terminal as the root/admin that why it's work there and not on the php, so just make sure to set permission to 777

Zack Heisenberg
  • 499
  • 6
  • 12