ls output changing when used through exec()

Posted by user359650 on Stack Overflow See other posts from Stack Overflow or by user359650
Published on 2011-06-24T14:59:17Z Indexed on 2011/06/24 16:22 UTC
Read the original article Hit count: 300

Filed under:
|
|
|
|

I'm using the ls command via PHP and exec() and I get a different output than when I run the same command via the shell. When running ls through PHP the year and month of the date get changed into the month name:

Running the command through the shell:

$ ls -lh /path/to/file
 -rw-r--r-- 1 sysadmin sysadmin 36M 2011-05-18 13:25 file

Running the command via PHP:

<?php
exec("ls -lh /path/to/file", $output);
print_r($output);

/*
Array
(
    [0] => -rw-r--r-- 1 sysadmin sysadmin  36M May 18 13:25 file
)
*/

Please note that:
-the issue doesn't occur when I run the PHP script via the cli (it only occurs when run through apache)
-I checked the source code of the page to make sure that what I was seeing was what I was getting (and I do get the month name instead of the proper date)
-I also run the ls command through the shell as the www-data user to see if ls was giving different output depending on the user (the output is the always the same from the shell, that is I get the date in yyyy-mm-dd instead of the month name)

© Stack Overflow or respective owner

Related posts about php

Related posts about date