0

I have a php-file that starts a java process which generates a file and should be called via http://www.servername.com/script.php. When I try php script.php from the console the file gets written correctly. If I start it via the browser there are problems with the encoding of the filename. It seems, that www-data (which is the user of the php script when I start it via the browser) has not the right locale.

Does anyone know how to set the locale for www-data?

I am on ubuntu trusty, php 5.5, java 1.7

Thanks, Sven

Chandru
  • 1,306
  • 13
  • 21
svendeswan
  • 75
  • 1
  • 9

3 Answers3

0

When you are trying to run this file from browser it runs as apache user. But when you run this file from CLI it works as expected. You may do one thing though, change the owner of that directory where the file will generate.

chown -R www-data:www-data [you path]

Hope it will works.

Or try executing root commands via PHP

Community
  • 1
  • 1
Bokul
  • 295
  • 1
  • 9
  • Thank you for your answer. Unfortunately it did not work... I think I need to set the locale for the Apache user somewhere, but I don't know where to do that.... – svendeswan Sep 11 '14 at 12:59
  • Then try this. Its works for me. http://stackoverflow.com/questions/8532304/execute-root-commands-via-php – Bokul Sep 11 '14 at 15:19
0

Use "sudo" command for execute your script. For example:

  1. create exec.php:

    <?php
    file_put_contents("/var/www/html/run.log", "run!\n"); 
    exec("/usr/bin/sudo /var/www/html/exec.sh", $out);
    file_put_contents("/var/www/html/run.log", var_export($out,true), FILE_APPEND); 
    ?>
    
  2. create exec.sh:

    #!/bin/sh
    #set you locale:
    export LC_ALL=en_EN.UTF-8
    #file that start a java process:
    /usr/bin/php -q /var/www/html/script.php
    
  3. edit /etc/sudoers via command "visudo", it's allows to execute script without su password:

    www-data ALL=(ALL) NOPASSWD: /var/www/html/exec.sh
    
  4. called script.php via exec.php (exec.php->exec.sh->script.php):

    curl 'http://www.servername.com/exec.php'
    
  5. debug:

    more /var/www/html/run.log
    
alchemist
  • 37
  • 3
0

Set locale in PHP script Maybe you can always change the locale on top of the script, like this:

setlocale(LC_ALL, 'en_US');

PHP config

use phpinfo() to see which PHP ini is loaded. When you call the PHP script from the CLI there might be another config with other locale settings

khoekman
  • 1,153
  • 7
  • 16