0

I am trying to run a Python script inside PHP and show the output.

I tried with simple test.py inside PHP, which print hello world without problem. But when I try to execute my desired command from PHP script the exec() returns empty string instead of output.

So the here is my php script. Below snippets works fine:

$output = exec("python test.py"); 
var_dump($output);

But not the desired one.

$command = "python -m scripts.label_image --graph=tf_files/retrained_graph.pb --image=".$uploadfile;
$output = exec($command);
var_dump($output);

so the folder structure is:

/var/www/mysite/
              -scripts(folder which contains python scripts)
              -tf_files(folder with other files)
              -uploads (image folder)

Can someone tell me what is going wrong?

Sample output in stdout/shell:

shell output of python script

17
  • Where and how is the desired one being called? Commented Jul 2, 2018 at 11:20
  • @Dave Hi Dave, I am uploading a image file using a form and which is processed in upload.php file. I am also executing the command in upload.php. The upload.php file is in parent folder mysite. Commented Jul 2, 2018 at 11:23
  • Is the path to where Python is installed visible to the web server? For the version you posted that works fine where and how did you run it? Commented Jul 2, 2018 at 11:24
  • Hi dave, I am using ubuntu 18.04. The path to python is "/usr/bin/python" which is included at the beginning of every python script i used. Although I did not understand what you meant by "where Python is installed visible to the web server". But as test.py can run successfully from localhost I guess python is visible to web server. Does it answer your question? Commented Jul 2, 2018 at 11:36
  • 1
    Is your script actually printing anything to its stdout ? Commented Jul 2, 2018 at 11:48

1 Answer 1

0

the variable $output should an argument. If you do this, you should get only the last output of your script.

exec("python test.py", $output); 
var_dump($output);

https://www.php.net/manual/en/function.exec.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.