0

I am trying to use exec command in PHP as follows:

exec("msgfmt /locale/2019_11_26_17.po -o /locale/file.mo")

It is not allowing me to do it. However, if I run this command using terminal, it works.

Please help me to figure out what is going wrong?

Thank you, Trupti

Trupti
  • 843
  • 2
  • 11
  • 28
  • You might have to use the absolute path for `msgfmt` (you can get it with `which msgfmt`). And make sure you are allowed to access it. The command will run under the user that runs the PHP script, i.e. if the script is executed from a website the user would be `www-data` (assuming the webserver is NGINX). – Tox Nov 26 '19 at 13:56
  • It is showing user as nobody. Can we change it to www-data? – Trupti Nov 27 '19 at 05:31
  • `chown www-data:www-data some_file` changes the file owner AND group to `www-data`, if that is what you mean – Tox Nov 27 '19 at 06:21
  • I tried to change the owner and group using PHP chwon function. However, it is not allowing me to do so. It returns false. – Trupti Nov 27 '19 at 07:54
  • You need to do it using the terminal with the `root` user or using `sudo`. – Tox Nov 27 '19 at 08:32
  • It is on shared host. I don't have root access. Also, I can not use sudo. Is there any way through php script we can do? – Trupti Nov 27 '19 at 08:46
  • Can you run the script via CLI? Then you could confirm that it works with your user account. If it doesn't, then the issue lies elsewhere. If it does, I would contact your hoster and ask them for advise. – Tox Nov 27 '19 at 13:02
  • No, I am not able to run the script through CLI. Giving error, PHP not found. However, if I run msgfmt command only, then it works through terminal. – Trupti Nov 28 '19 at 07:35
  • You can try if you get an error message with `echo exec(...)`. But I would ask the hoster because it sounds like PHP can not run `msgfmt` and you lack the permissions to change the ownership. It could also be that your hoster disabled the use of exec entirely. In both cases they can enable it for you. – Tox Nov 28 '19 at 08:13

1 Answers1

0

Try that:

exec("msgfmt /locale/2019_11_26_17.po -o /locale/file.mo");

" <- is important for function.

Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34