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 :
- check if shell_exect is exist
if(function_exists('exec')) {
echo "exec is enabled";
}
if(function_exists('shell_exec')) {
echo "shell_exec is enabled";
}
- Prove shell exect is working
if (exec('echo TEST') == 'TEST')
{
echo 'exec works!';
}
output : exec works! exec is enabled shell_exec is enabled"
- Added handler script in httpd.conf in config xampp
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
- 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
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?