0

I have php code and python code and i use visual studio code as code editor, i use xampp apache for personal server. The code is below :

Html run button

<form action="" method="post">
          <div class="card-header py-3 mb-30 mt-20 pb-20 pt-20">
                <div class="center">
                <input type="submit" name="runpython" value="Run" class="btn btn-primary">
          </div>
</div>
</form>

This is the run python by php

<?php 
if(isset($_POST['runpython'])){
   $output = shell_exec('python3 /xampp/htdocs/Klasifikasi_KNN/admin test.py');
   echo $output;
   echo shell_exec('/xampp/htdocs/Klasifikasi_KNN/admin test.py');
   $output = shell_exec('python3 /xampp/htdocs/Klasifikasi_KNN/admin test.py');
   echo $output;
   echo shell_exec('xampp/htdocs/Klasifikasi_KNN/admin test.py');
   echo shell_exec('/xampp/htdocs/Klasifikasi_KNN/admin test.py');
   echo shell_exec('/xampp/htdocs/Klasifikasi_KNN/admin/test.py');
   echo shell_exec('xampp/htdocs/Klasifikasi_KNN/admin/test.py');
   echo shell_exec('python3 xampp/htdocs/Klasifikasi_KNN/admin/test.py');
   echo shell_exec('python3 /xampp/htdocs/Klasifikasi_KNN/admin/test.py');
}
?>

This is the test.py

#! C:/Users/Jessen PC/AppData/Local/Microsoft/WindowsApps/python.exe
print("Hello World")

What ive tried :

  1. check if shell_exect is exist
    if(function_exists('exec')) {
        echo "exec is enabled";
    }
    if(function_exists('shell_exec')) {
        echo "shell_exec is enabled";
    }
  1. Prove shell exect is working
if (exec('echo TEST') == 'TEST')
{
    echo 'exec works!';
}

output : exec works! exec is enabled shell_exec is enabled"

  1. Added handler script in httpd.conf in config xampp
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
  1. If i use D drive like this
$output = shell_exec('D:/xampp/htdocs/Klasifikasi_KNN/admin/test.py');
echo $output;

This open like image below output What im aiming is the code is debug by python and display the output in website.

Is there something missing needs to be done to execute python files from php?

Jessen Jie
  • 167
  • 1
  • 12
  • You might want to prove that `shell_exec` is working first? Verify that you can run the basic example as per [thread](https://stackoverflow.com/questions/19735250/running-a-python-script-from-php). – metatoaster Jan 10 '23 at 06:08
  • Although your button has `name="runpython"`, that does not cause the POST request to have a variable named `runpython`. So `if(isset($_POST['runpython']))` is false and your Python won't run. Add something to your form like `` – BareNakedCoder Jan 10 '23 at 06:15
  • @metatoaster yes i have prove it and edited the questions thankyou, the shell exect is working fine. – Jessen Jie Jan 10 '23 at 06:21
  • @BareNakedCoder I don't see how `if(isset($_POST['runpython']))` could be false when the form has been submitted. There _is_ an element with `name="runpython"`. Using a hidden input wouldn't make a difference – brombeer Jan 10 '23 at 06:41
  • Have you tried using the complete path to `python` _and_ the complete path to your script? – brombeer Jan 10 '23 at 06:43
  • @brombeer is this what you mean by complete path to python? #! C:/Users/Jessen PC/AppData/Local/Microsoft/WindowsApps/python.exe and this is complete path to the script? $output = shell_exec('D:/xampp/htdocs/Klasifikasi_KNN/admin/test.py'); echo $output; – Jessen Jie Jan 10 '23 at 07:04
  • 1
    Something like `$output = shell_exec('C:/Users/Jessen PC/AppData/Local/Microsoft/WindowsApps/python.exe D:/xampp/htdocs/Klasifikasi_KNN/admin/test.py'); echo $output;` – brombeer Jan 10 '23 at 07:16
  • @brombeer Its only open the file on vscode not executing the script and display the output in website – Jessen Jie Jan 10 '23 at 07:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250996/discussion-between-jessen-jie-and-brombeer). – Jessen Jie Jan 10 '23 at 07:42
  • @brombeer Thankyou i try using anaconda python installation path like this echo shell_exec('D:\Anaconda\python.exe D:/xampp/htdocs/Klasifikasi_KNN/admin/test.py'); now it works. – Jessen Jie Jan 10 '23 at 08:01
  • Nice, grats. Glad it works! ;) – brombeer Jan 10 '23 at 08:02

0 Answers0