0

I have code like this in php:

header("Content-Type: text/plain");
exec("/bin/bash -c 'ls --color=always'", $result);
echo implode("\n", $result);

and I've got result (escape is not visible)

Makefile
README
Yann Tiersen - Comptine d'un autre ete L'apres midi.mp3
bush-src.js
bush.js
bush.min.js
[0m[01;34mcgi-bin[0m
config.json
[01;34mcss[0m
favicon.ico
foo.png
index.php
[01;34mlib[0m
ls.txt
test.php
test.php~
[01;32mtest.txt[0m

It show ANSI escape codes for directories and executable files, but when I call from command line

php -r "exec('bash -c \"ls --color=always\"', \$result);echo implode(\"\\n\", \$result);" | less

I've got this:

bush.js
bush.min.js
bush-src.js
ESC[0mESC[01;34mcgi-binESC[0m
config.json
ESC[01;34mcssESC[0m
favicon.ico
ESC[01;35mfoo.pngESC[0m
index.php
ESC[01;34mlibESC[0m
ls.txt
Makefile
README
test.php
test.php~
ESC[01;32mtest.txtESC[0m
ESC[00;36mYann Tiersen - Comptine d'un autre ete L'apres midi.mp3ESC[0m

png and mp3 have ANSI escapes codes, why php exec from webpage don't give that as return?

2
  • You probably have a $LS_COLORS variable defined somewhere in your ~/.bashrc/~/.zshrc... (probably via a call of eval "$(dircolors)") Feb 19, 2014 at 12:59
  • @StephaneChazelas You're right, I have that if [ -x /usr/bin/dircolors ]; then eval "dircolors -b" fi
    – jcubic
    Feb 19, 2014 at 13:09

3 Answers 3

1

You probably have a $LS_COLORS environment variable defined somewhere in your ~/.bashrc/~/.zshrc... (probably via a call of eval "$(dircolors)").

The php that you run on the command-line will inherit your shell's environment, including that $LS_COLORS variable.

While the one started by apache2 will inherit apache2's environment which is unlikely to have $LS_COLORS in it. bash doesn't read ~/.bashrc (and anyway that would be the ~/.bashrc of the user running apache if it did) when not run interactively (except in corner cases like when invoked by sshd/rshd/telnetd).

Note that php's exec() already starts a shell, so you don't need to start another one (especially bash which is the slowest shell in the west).

 exec('ls --color=always', $result);

should be enough.

$ strace -fe execve php -r 'exec("bash -c echo\ test");'
execve("/usr/bin/php", ["php", "-r", "exec(\"bash -c echo\\ test\");"], [/* 49 vars */]) = 0
[pid  3608] execve("/bin/sh", ["sh", "-c", "bash -c echo\\ test"], [/* 49 vars */]) = 0
[pid  3608] execve("/bin/bash", ["bash", "-c", "echo test"], [/* 48 vars */]) = 0
2
0

that's because it's not sending html but text

7
  • Don't undertand, this is test code that send ANSI escape but they are not send for mp3 and png files.
    – jcubic
    Feb 19, 2014 at 12:46
  • check the html in output and post that result here
    – sathia
    Feb 19, 2014 at 12:52
  • I have text/plain as output I put just that. It's not html related at all.
    – jcubic
    Feb 19, 2014 at 12:53
  • and how would you expect this to be colored if you are using a browser?
    – sathia
    Feb 19, 2014 at 12:55
  • I ask about ANSI escape sequences that are not send to browser. I've replaced color with ANSI escape in question.
    – jcubic
    Feb 19, 2014 at 12:58
0

In my .bashrc file there is:

if [ -x /usr/bin/dircolors ]; then
    eval "`dircolors -b`"
fi

which color the files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .