0

I have developed the following PHP code in order to read a specific directory called /dados/capture:

tst2.php

<html>
<body>
<?php
echo "whoami: " . shell_exec('whoami')."<br>\n";
exec('ls /dados/capture/',$base);
echo "/dados/capture content ...<br>\n";
foreach ( $base as $dir )
{
    print $dir ."<br>\n";
}
?>

Executing the code through linux command line "[root$mysite]# sudo -u apache php tst2.php" returns the following:

<html>
<body>
whoami: apache
<br>
/dados/capture content ...<br>
120130312115149.jpg<br>
120130312115149.swf<br>
120130312115658.jpg<br>
120130312115658.swf<br>
</body>
</html>

Calling tst2.php from web browser results in:

whoami: apache 
/dados/capture content ...

I have tried changing the permission for the folder(s) and creating directive <directory><directory> in httpd.conf, without any change to the result.

I have tested the code with different directory it was a surprise, because it also doesn't work for /boot, /mnt and /opt directories.

Using glob or readdir result the same, but consumes much more processor.

I'm using CentOs 2.6.32-279.22.1.el6.i686, Apache/2.2.15 and PHP 5.4.12.

Did someone have a similar problem?

Michael Härtl
  • 8,428
  • 5
  • 35
  • 62
Mauricio
  • 1
  • 1
  • if you could place your code in codeblocks please. to use a codeblock leave 4 spaces – Nikitas Mar 21 '13 at 17:44
  • 1
    It seems a permissions problem. On the command line you are running the script as root. The apache server user probably has not enough rights to list the folder – Ateszki Mar 21 '13 at 17:45

1 Answers1

0

Try
$out = shell_exec('ls /dir/path 2>&1'); The user who is browsing the web may not have permission. So, you may be getting this error.