0

From PHP which interact with the VPS I can do:

$command = '/usr/bin/python3 say_hello.py';
$command = escapeshellcmd($command);
system($command);

with say_hello.py :

#!/usr/bin/python3
print("Hello")
print("<br>")

From terminal I can do :

/usr/bin/python3 rotate.py;

with rotate.py :

#!/usr/bin/python3
from PIL import Image

with Image.open("rotate_before.png") as im:
    data = list(im.rotate(45).getdata())
    imNew = Image.new(im.mode, im.size)
    imNew.putdata(data)
    imNew.save("rotate_after.png")

But from PHP I don't get "rotate_after.png" with this command line :

$command = '/usr/bin/python3 rotate.py';
$command = escapeshellcmd($command);
system($command);

it is the same with

shell_exec($command);

with

$command = '/usr/bin/python3 rotate.py';<br>
$command = escapeshellcmd($command);
system($command, $retval);
var_dump($retval);

I get:

int(1) 

SOLUTION : With what has been said below :

For security I have kept chmod -R 0755 and added www-data for the dir_web-site

How do I give PHP write access to a directory?

  • did you get any error ? your code seems valid – Medo Jun 07 '23 at 20:03
  • Invoking Python from a PHP process is dangerous to say the least. Aside from that, my guess is your PHP process user may not have permissions to create files as the Python script is doing, or quite simply, the file IS created but within a different directory than you expect (it would be the working directory of the PHP process). – h0r53 Jun 07 '23 at 20:03
  • With system($command, $retval); and var_dump($retval); I will get int(1) – user7058377 Jun 07 '23 at 20:27
  • rotate_after.png is not in the root of index.php – user7058377 Jun 07 '23 at 20:31
  • YES!! It was the right permission. I have done chmod 0777 dir_where_index.php_is. – user7058377 Jun 07 '23 at 20:56
  • I want to do a python script to make a graphic and print it with PHP in HTML – user7058377 Jun 07 '23 at 20:57
  • For security I have done cmod -R 0755 and added www-data for the dir_web-site https://stackoverflow.com/questions/2900690/how-do-i-give-php-write-access-to-a-directory – user7058377 Jun 07 '23 at 21:25

0 Answers0