20

I need to address UTF-8 filenames with the php exec command. The problem is that the php exec command does not seem to understand utf-8. I use something like this:

echo exec('locale charmap');

returns ANSI_X3.4-1968

looking at this SO question, the solution lookes like that:

echo exec('LANG=de_DE.utf8; locale charmap'); 

But I still get the same output: ANSI_X3.4-1968

On the other hand - if I execute this php command on the bash command line:

php -r "echo exec('LANG=de_DE.UTF8 locale charmap');"

The output is UTF-8. So the questions are:

  1. Why is there an different result be executing the php command at bash and at apache_module/web page?
  2. How to set UTF-8 for exec if it runs inside a website as apache module?
3
  • Wait, are you trying to change the locale of the process spawned by the exec, or are you trying to get the exec to change PHP's locale? Commented Dec 20, 2012 at 4:22
  • @Charles i'm not sure, what exactly you mean. But i found out the following solution (take a look below). Commented Dec 20, 2012 at 10:16
  • How did you found out what that your exec command returns ANSI_X3.4-1968 Commented Nov 17, 2015 at 12:08

3 Answers 3

43

To answer my own question - i found the following solution:

setting the locale environment variable with PHP

$locale='de_DE.UTF-8';
setlocale(LC_ALL,$locale);
putenv('LC_ALL='.$locale);
echo exec('locale charmap');

This sets to / returns UTF-8. So i'm able to pass special characters and umlauts to linux shell commands.

Sign up to request clarification or add additional context in comments.

7 Comments

finding this makes me one happy man right now. thanks for the q&a
This is great. I'd set the charset in a config file in /etc but after a server-upgrade from Ubuntu 12.04 to 14.04, that config was apparently gone. So it's better to set it in the PHP file to avoid future problems it seems.
Note that you can also specify LC_ALL=... right in front of your exec call (like in the original post with the wrong environment variables) so that it affects only that one call if needed, e.g. exec('LC_ALL=de_DE.UTF-8 locale charmap') - sometimes, this might look cleaner.
Thank you. This helped me a lot. I don't understand how it is possible for a language which is supposed to be "modern" and is pretty much the most widely used language in the world to default to a charset designed in 1968!
putenv('LC_ALL=cs_CZ.utf-8') did the trick for me. suddenly my command with czech characters is not corrupted
|
5

This solves it for me (source: this comment here):

<?php
putenv('LANG=en_US.UTF-8'); 
$command = escapeshellcmd('python3 myscript.py');
$output = shell_exec($command);
echo $output;
?>

2 Comments

Solved exactly my problem. Thumbs up for the documentation!
I got invalid input found on input connection in R language source()called by php. php-> shell ->R.You save me :)
2

I had the similar problem. My program was returning me some German letters like: üäöß. Here is my code:

$programResult = shell_exec('my script');

Variable $programResult is containing German umlauts, but they were badly encoded. In order to encode it properly you can call utf8_encode() function.

$programResult = shell_exec('my script');
$programResult = utf8_encode($programResult);

Comments

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.