4

I want run php ../cat1/index.php gr_s2/3/gr-n40 1200 command by php. The shell_exec return NULL as result but when I try that command on cmd, output was shown correctly.

What happen in php and shell_exec ?!

Note: The command with different parameters (Like: php ../cat1/index.php gr_s2/3/gr-n40 800) works correctly in both (php and cmd).

Masoud Nazari
  • 476
  • 2
  • 6
  • 17

2 Answers2

3

There is a note in php manual page of shell_execute:

Note: This function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required.

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

So your code running with error. Try with exec. If you want, insert your code (or blocks) to be checked.

shozdeh
  • 172
  • 1
  • 11
0

This is fixed by adding user used by web-server to sudoer and run the php command with

sudo php ..........

To start run this command sudo visudo

Add the following line at the end of sudoer file opened

www-data ALL=NOPASSWD: /usr/bin/php

But if you whant to execute all commad from php add this line instead of the above www-data ALL=NOPASSWD: ALL which is not recommended

and the run your commands with sudo php /path/to/you/file.php

For my case I was running ubuntu 14.04

Have fun please

Ruberandinda Patience
  • 3,435
  • 3
  • 20
  • 18