-1

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.

  • 1
    help you out with what? ... all we know is the location of your files, nothing else .... you have posted almost zero information that can be used to answer your question .... your question is similar to my car won't start, please help me – jsotola Mar 26 '18 at 5:46
  • Does your python script run stand-alone, e.g. python myscript.py? – Ingo Mar 26 '18 at 10:25
1

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. python.py:

#!/usr/bin/env python
# your python code here

Make your script executable: chmod +x python.py

2.) Create the html file with button or 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 arg1, 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.

0

I suppose you are looking for the PHP system function : http://php.net/manual/fr/function.system.php

Sample code :

<?php $result_last_line = system("python /my/path/myScript.py"); echo "Result : ".$result_last_line; ?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.