2

I have this php script:

<?php
  $cmd_desformat = "/usr/local/bin/process /tmp/input.txt > /tmp/output.txt";
  shell_exec($cmd_desformat);

Where input.txt is a UTF-8 file (checked with "file -i") containing:

Buenos días

and /usr/local/bin/process is a binary executable from a third party which I have extensively used and never got this problem.

Well, problem is that when I execute this from a browser:

http://localhost/test.php

The result output.txt is an US-ASCII file containing:

Buenos d?as [][

However, when I execute this from command line:

php test.php

The result output.txt is an UTF-8 file with the expected:

Buenos días [][

I have tried to execute from command line with user www-data to see if I could replicate the browser behaviour, but the result is again the correct one. I have also tried to use exec instead of shell_exec but the results are the same. Also tried with Firefox and Chrome.

I need it to work when being called from the browser. Any ideas?

1
  • Is bin/process sensitive to the environment locales…? Commented May 7, 2018 at 15:59

1 Answer 1

5

The PHP CLI environment is not the same as the shell_exec one. There are two possibilities for how to manage to get your content to return in the proper manner.

The easiest way is to just reset the environment by calling env -i in the shell_exec call.

<?php
  $cmd_desformat = "env -i /usr/local/bin/process /tmp/input.txt > /tmp/output.txt";
  shell_exec($cmd_desformat);

This may not work if the default environment is not correctly set up. If that is the case, then you may need to set it up explicitly using putenv().

<?php
  putenv('LANG=en_US.UTF-8'); // Set this to the language you need
  $cmd_desformat = "/usr/local/bin/process /tmp/input.txt > /tmp/output.txt";
  shell_exec($cmd_desformat);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you mate!! I was pulling my hair on this one... I used putenv and then it worked fine. OMG I was beginning to lose my sanity 😂 I don't understand how it's not a default in PHP...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.