1

I am using php to have a button refresh a python script. The goal of this is to when a certain input is present and a button is pressed, to run the python script. If there is no user input, there is a default value for $input_val, just incase. The way I have been trying to test this is in my python file, I have a line that should output to var/tmp/my_logfile.log, but no logs are being returned when run through the php page. Like every other stack article I've seen it works in the command prompt.

The code I use to try this has been the following:

PHP:

if (isset($_POST['update']))
{
    exec('/usr/bin/python3 /var/www/python_test.py ".$input_val."');
}

HTML:

<form name="update" method="post" >
    <button name = "update" type="submit"> Update charts </button>
</form>

Some of the references I have used, but to no luck:

Execute Python script from Php

HTML form button to run PHP to execute Python script

https://www.raspberrypi.org/forums/viewtopic.php?t=105932

https://serverfault.com/questions/679198/executing-a-python-script-through-php-button

Travis
  • 657
  • 6
  • 24
  • Presumably the Python script fails to run. Check exec() manual page. Use the 2nd and 3rd optional parameters to debug. – marekful Jul 23 '18 at 14:24
  • Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Nigel Ren Jul 23 '18 at 14:40

1 Answers1

2

You can't put a variable in a single quote string, unless that is what you actually want. Instead, either break out and concatenate, or use double quotes:

<?php exec('/usr/bin/python3 /var/www/python_test.py "' . $input_val . '"'); // note extra single quotes

Or:

<?php exec("/usr/bin/python3 /var/www/python_test.py \"$input_val\""); 

The above codes presume you actually type the double quotes in the terminal.

If not, just get rid of them like so:

<?php exec('/usr/bin/python3 /var/www/python_test.py ' . $input_val );

Or:

<?php exec("/usr/bin/python3 /var/www/python_test.py $input_val");
delboy1978uk
  • 12,118
  • 2
  • 21
  • 39