0

I wanna run python code in PHP and the PHP code is bellow:

<?php
$command = escapeshellcmd('python3 /usr/C:/xampp/htdocs/scripts/a.py');
$output = shell_exec($command);
print $output;

But, when I run this PHP code, nothing happens in the screen!

How can I fix it?

Parisa Mousavi
  • 52
  • 1
  • 11

2 Answers2

0

If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().

See http://www.php.net/manual/en/ini.core.php#ini.disable-functions

Your apache's php.ini file may look something like

disable_functions=exec,passthru,shell_exec,system,proc_open,popen

Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.

Cosmin
  • 478
  • 1
  • 5
  • 16
0

The problem wasn't because of disable_functions and the above code doesn't have any problem in Linux.

So, I think the problem is because of windows or the directory!

Parisa Mousavi
  • 52
  • 1
  • 11
  • `shell_exec` should run on windows, more than likely the path is incorrect. See: https://stackoverflow.com/questions/13783649/shell-exec-with-windows-path-not-running – nickl- Mar 16 '19 at 21:54
  • Try running something simple like: `echo this is a test`. – nickl- Mar 16 '19 at 23:36
  • @nickl- There is no problem! I think the problem is with `shell_exec`! – Parisa Mousavi Mar 17 '19 at 07:37
  • Then we better fix it! What do you say... =) – nickl- Mar 17 '19 at 07:50
  • @nickl- What do you mean? – Parisa Mousavi Mar 17 '19 at 20:49
  • You said: the problem is with shell_exec. So I said: Then we better fix it! What do you say... =) If it's broke we fix it, that's what we do. Any idea what is wrong with it? – nickl- Mar 18 '19 at 00:09
  • @nickl- I tried many different things. But none of them gave me the right answer! – Parisa Mousavi Mar 18 '19 at 09:07
  • Edit your question and explain to us what you tried and what results you got. This will make it easier to assist you. `exec` is a different api method from `shell_exec` for executing shell commands, did you also try it. – nickl- Mar 19 '19 at 05:55