I have all files placed under var/www/html folder. When I click my button on web page the php file must run the python file and give the ouput at the GPIO pins. But nothing is happening so. Anybody please help me out. Thank u :)
closed as unclear what you're asking by joan, Milliways, Dmitry Grigoryev, Darth Vader♦ Mar 26 '18 at 14:03
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
In case your web server runs as user
root (not always a great idea, but that is off-topic) you can run your *.py from *.php this way:
1.) Create the python file, i.e.
#!/usr/bin/env python # your python code here
Make your script executable:
chmod +x python.py
2.) Create the html file with
a href, i.e.
page.html (optional, you could request the following php file straight):
<!-- your html here --> <a href="piggyback.php">click me to run python via php</a> <!-- more html there -->
3.) create the php file, i.e.
piggyback.php and attach the appropriate output (replace
arg2) for the GPIOs:
<?php exec('sudo python /path/to/python.py arg1 arg2'); ?>
Addition: If you are looking for a more secure solution, please read Stack Overflow - Running a Python script from PHP.
I suppose you are looking for the PHP
system function :
Sample code :
$result_last_line = system("python /my/path/myScript.py");
echo "Result : ".$result_last_line;