0

I've create PHP script that takes in user input and sends it to a Python script. The Python script creates an image which the PHP script displays.

Here's my Python code:

import sys
import matplotlib.pyplot as plt
import numpy as np
import ftplib
result = sys.argv[1]
x = np.arange(0, result, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.savefig('image.png')

My PHP code:

<!DOCTYPE html>
<html lang="en">
<head>
 <title>Contact Form</title>
</head>
<body>
 <h2>Value identifier</h2>
 <p>Please fill in the value:</p>
 <form action="" method="post">
 <p>
 <label for="inputName">Name:</label>
 <input type="text" name="value" id="inputName">
 </p>
 <input type="submit" name="submit" value="Submit">
 </form>
</body>
</html>
<?php
 error_reporting(E_ALL); 
 ini_set('display_errors', 1);
 $values=$_POST['value'];
 $rad=exec("python test.py".$values);
 echo $values;
 echo $rad;
 echo "<img src='image.png'>";
?>

I don't get anything published, as if the Python script isn't even running. But printing the values I want to pass is successful.

miken32
42.5k16 gold badges127 silver badges177 bronze badges
asked Feb 4, 2021 at 22:19

2 Answers 2

0

The key line:

$rad=exec("python test.py".$values);
``
shouldn't be concatenated. The period should be a comma. Also, you don't need python before test.py. You do need the filepath, however. Also, you may want to try shell_exec() instead of exec() because that will just return a string, and you can print out the string to see what you are getting. See below:

$rad = shell_exec('/home/path/path/path/RELATE/main.py')

Separately, this question is similar to the one you are asking and gives you a separate way to get PhP to talk to Python using a text file as an intermediary. The benefit of this is that it would at least help you identify whether you have a PhP problem or a Python problem when there is a bug in your code.
https://stackoverflow.com/q/47981370/9807545
answered Feb 4, 2021 at 22:59
Sign up to request clarification or add additional context in comments.

4 Comments

No they aren't the same question ... can you show me a fixed $rad=exec("python test.py".$values);I don't understand what you mean by concetrated ?
Sorry, so 'concatenate' means adding one string to another. In python it would be str = "Hello " + "World!" and in PhP its $str = "Hello " . "World!". According to the PhP Manual, the syntax for 'exec()' is exec ( string $command , array &$output = null , int &$result_code = null ). The second argument is separated from the first by a 'comma' not a period.
P.S. - The reason I gave you the link to the other question and answer is that it gives you an alternative way to communicate the contents of $values to your python code. If you just have PhP write it to a .txt file and Python read the values from that .txt file and create the image you want, then, when Python is done, the operating system will return to PhP and carry on with the echo statements.
Separately, you may also want to try and import traceback into your Python and print any tracebacks to a .txt file so you can debug your Python code in the event there is a mistake in your Python code which throws an error you are not seeing.
0

You are concatenating your arguments together without a space between them. Further, you must always escape values before passing them to the shell. Use escapeshellargs() for this.

<?php
if (isset($_POST["value"])) {
 $value = $_POST["value"];
 $rad = exec("python test.py ". escapeshellargs($value));
 echo $value;
 echo $rad;
 echo "<img src='image.png'>";
}
?>

You will also need to ensure that the user running the web server process has write permissions to the directory the scripts are contained in. This is unlikely to be the case, and certainly should not be the case. I'd suggest setting up a separate directory with appropriate permissions.

answered Feb 5, 2021 at 0:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.