0

i am trying to execute a python script via a php script. both scripts running on my web host from the same folder. this first example works perfectly.

<?php
shell_exec("python main.py");
echo"success";
?>
import os

os.popen('cp uploads/1.jpg sessions/1.jpg')

here comes the problem, when i try to run a slightly more complex python script i get no results?

same php file

new python script

import cv2
import datetime
import pytz

tz_London = pytz.timezone('Europe/London')
datetime_London = datetime.datetime.now(tz_London)
name = datetime.date.today().strftime("%y-%m-%d-") + (datetime_London.strftime("%p"))
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')

width = 295
height = 360
dim = (width, height)

filename = "sessions/%s_1.jpg" % name
img = cv2.imread('uploads/1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x +100 + w -100, y +100 + h -50), (0, 0, 255), 2)
    faces = img[y:y +50 + h, x:x + w]
    faces = cv2.resize(faces, dim)
    cv2.imwrite(filename, faces)

print(name)

by the way, when i execute this python script via the terminal it works perfectly.

any help would be much appreciated

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
Piet Bez
  • 95
  • 1
  • 2
  • 7
  • 3
    why dont you just create a api of this python script and use curl request to call it – Hammad Ahmed khan Dec 02 '22 at 06:45
  • is there any way of executing the python script with the method above? without api? i dont understand why my first script works but the second one doesn't? – Piet Bez Dec 04 '22 at 21:26

1 Answers1

0

You can try pass through it Execute an external program and display raw output

passthru();

Source: http://php.net/manual/en/function.exec.php

or you could just use the exec() command :

My test.py

#!/usr/bin/env python3.5
print("Hello World!")

php I now do this:

$message = exec("/var/www/scripts/test.py 2>&1");
print_r($message);

result : Hello World!

Hammad Ahmed khan
  • 1,618
  • 7
  • 19