1

I am making an android application in which I am first uploading the image to the server and on the server side, I want to execute a Python script from PHP. But I am not getting any output. When I access the Python script from the command prompt and run python TestCode.py it runs successfully and gives the desired output. I'm running Python script from PHP using the following command:

$result = exec('/usr/bin/python /var/www/html/Source/TestCode.py');

echo $result

However, if I run a simple Python program from PHP it works.

PHP has the permissions to access and execute the file.

Is there something which I am missing here?

3
  • exec doesn't print by itself, it will return the last line of output or it will put all lines of output into the second parameter (if provided), as the manual page would have told you ;o/ Commented Aug 10, 2016 at 9:39
  • 1
    I modified it and still the problem is there. Any suggestions?? Commented Aug 10, 2016 at 12:37
  • do you by any chance know the difference between stderr and stdout? because exec - as far as I know - only handles stdout. If your testcode.py only produces stderr output, you'll see nothing. Commented Aug 10, 2016 at 14:06

2 Answers 2

3
exec('/usr/bin/python /var/www/html/Source/TestCode.py', $output);

var_dump($output);

2nd Parameter of exec will give output

EDIT:

exec('/usr/bin/python /var/www/html/Source/TestCode.py 2>&1', $output);

2>&1 - redirecting stderr to stdout. Now in case of any error too, $output will be populated.

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

1 Comment

I received the output array(0) {}, which means that my array is empty but when I am running the python script it gives me the output.
1
  1. First Check your python PATH using "which python" command and check result is /usr/bin/python.

  2. Check your "TestCode.py" if you have written #!/usr/bin/sh than replace it with #!/usr/bin/bash.

  3. Than run these commands exec('/usr/bin/python /var/www/html/Source/TestCode.py', $result); echo $result

2 Comments

I didn't get any output.
Ok just send your TestCode.py file, than let me try my best.

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.