I have a PHP file on my web server that is supposed to call a python script (same folder) that writes out to a file and then get its contents and displays the data.
The PHP file is called q.php
it contains
<?php
$tmp = exec("trivia.py");
sleep(4);
$homepage = file_get_contents('./testfile.txt', true);
echo $homepage + '<p>exec ret:' + $tmp;
echo exec("whoami");
?>
This file calls a trivia.py
which writes out a file ("./testfile.txt") and then php gets the data from the file and displays it. I added a variable to see if the exec is working and it returns 0
. The PHP server is being executed by user http
.
now for trivia.py
, I have the following line at the top of the file
#!/usr/bin/env python
and it executes perfectly fine when I SSH into the server. From SSH I run the script and it creates the file specified above and the web page works fine. However, if I used the PHP file to create it, it will not work from the web.
I am pretty sure this has something to do with permissions somewhere but I am not that great on permissions for Linux.
SYSTEM INFO: Synology Diskstation, DSM5, PHP5, Python 2.7
EDIT:
trivia.py
currenlty has 777 as permissions with admin
owner and group users
When running a file via exec you need to provide the full path to the file, it doesn't matter if the file is in the same directory.
Please try it with (assuming the file is in /var/www/public)
exec('/var/www/public/trivia.py');
For Windows User - Problem was resolved here PHP exec python
I suggest to replace $tmp = exec("trivia.py");
for this one
$tmp = exec("C:\\Python27\\python.exe trivia.py");
// Add your python.exe route before the file
Hope it works!!
</div>