RE: [PHP] I can't read directory contents
| From: | Peter Benoit | Date: | Thu, 29 Jun 2000 19:51:22 +0000 |
| Subject: | RE: [PHP] I can't read directory contents | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-3873@lists.php.net to get a copy of this message | ||
Ya, sometimes you see so much it just all looks confusing...
Your script returns the Error message every time, which I guess means I
can't read the contents of the directory?
Is there a permission that needs to be set for this? What if my host says
it's a security issue?
Thanks again,
Peter
-----Original Message-----
From: Ben Gollmer [mailto:ben@jatosoft.com]
Sent: Wednesday, June 28, 2000 3:54 PM
To: Peter Benoit
Cc: php-general@lists.php.net
Subject: Re: [PHP] I can't read directory contents
I think the problem lies in the exec( "ls -la $dir",$lines,$rc) line. ls
doesn't do anything under NT.
Try using the built-in filesystem functions in PHP. For example:
$theDir = @opendir($currentDir);
if (!$theDir)
{
print ("<b>Error: Cannot open directory $currentDir.</b><br>");
}
else
{
while($filename = readdir($theDir))
{
print $filename;
print ("<br>");
}
}
This will print out all the files in the directory $currentDir. Works under
both NT and Linux. Just add an ereg or two in there to match html files,
and you should be set.
Ben Gollmer
Jatosoft, LLC
http://www.jatosoft.com/
At 03:23 PM 6/29/2000 -0400, you wrote:
>Hi,
>
>I was using the following code on my Unix server which worked just fine.
>Now on my NT server, the host company states that I cannot read the
>directory contents and write them to this page because it's a security
>problem.
>
>Simply put, this should look for .htm files in a directory, and print them
>out on the page. It doesn't work now, but it works fine on my Unix host
>machine.
>
>Is this a security problem? Is there a setting I need on NT? Can't I do
>this with the standard read permissions?
>
>What do I need in order for a script like this to work?
>
>TIA,
>Peter
>
>
><?php
> exec( "ls -la $dir",$lines,$rc);
> $count = count($lines) - 1;
> for ($i = 1; $i <= $count; $i++) {
> $type = substr($lines[$i],0,1);
> $name = strrchr($lines[$i], " ");
> $name = substr($name,1);
> $dire = substr($lines[$i],0,strpos($lines[$i],$name));
> if (strpos($name, ".mp3")) {
> //echo "<font color=\"red\">";
> } else {
> if (strpos($name, ".php") or strpos($name, ".cgi")) {
> // echo "<font color=\"maroon\">";
> } else {
> if ($type == "d") {
> //echo "<font color=\"blue\">";
> } else {
> //echo "<font color=\"black\">";
> }
> }
> }
> //echo "$dire</font>";
> if ($type == "d") {
> if ($name == "." or $name == "..") {
> echo "";
> } else {
> if ($dir == "") {
> // echo "<a
> href=directory.php?dir=$name>$name</a><br>";
> } else {
> // echo "<a
>href=directory.php?dir=$dir/$name>$name</a><br>";
> }
> }
> } else {
> if ($dir == "" and !strpos($name, ".php") and !strpos($name,
>".text")) {
> echo " <P><font face=\"verdana,arial\">[<a
href=$name>view</A>]
>: <a href=source.php3?page_url=$name>$name</a><br>";
> } else {
> // echo " <P><font face=\"verdana,arial\">[<a
>href=$name>view</A>] : <a
>href=source.php3?page_url=$dir/$name>$name</a><br>";
> }
> }
> }
> ?>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
>For additional commands, e-mail: php-general-help@lists.php.net
>To contact the list administrators, e-mail: php-list-admin@lists.php.net