is_dir not recognising directories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

is_dir not recognising directories

Post by impulse() »

I have made a quick little script that can browse directories on my server and it was hopefully going to output all directories as green but also for each line displayed it would switch between bold and not bold font. The bold and not bold works but it isn't outputting the directories as green. Could somebody help me out please?

The code:

Code: Select all

echo "<form method = 'post' action = 'dir.php'>";
echo "<input type = 'text' name = 'directory'>";
echo "<input type = 'submit' value = 'Get contents'>";
echo "</form>";

$directory = $_POST["directory"];

$handle = opendir($directory);

$i = 0;
while ($file = readdir($handle)) {
  if ($i%2 == 1) {
    if (is_dir($file)) {
      echo "<b><font color = 'green'>", $file, "<font color = 'black'></b>";
    } else {
      echo "<b>", $file, "<br></b>";
    }
  }
    else {
    if (is_dir($file)) {
      echo "<font color = 'green'>", $file, "<font color = 'black'>";
    } else {
      echo $file, "<br>";
    }
}
$i++;
}
closedir($handle);
Is this due to my if structure?


Regards,
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

You have a problem in the HTML - you need to close the <font> tag: </font>

EDIT:
I tested the code below and it works fine:

Code: Select all

echo "<form method = 'post' action = 'dir.php'>";
echo "<input type = 'text' name = 'directory'>";
echo "<input type = 'submit' value = 'Get contents'>";
echo "</form>";

$directory = $_POST["directory"];
$handle = opendir($directory);

$i = 0;
while ($file = readdir($handle)) {
  if ($i%2 == 1) {
    if (is_dir($file)) {
      echo "<b><font color = 'green'>", $file, "</font></b><br />
";
    } else {
      echo "<b>", $file, "</b><br />
";
    }
  }
    else {
    if (is_dir($file)) {
      echo "<font color = 'green'>", $file, "</font><br />
";
    } else {
      echo $file, "<br>";
    }
}
$i++;
}
closedir($handle);
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Is still doesn't display any green.

If I put "echo 'foo'" within the is_dir condition it never displays it.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

First of all, put in $directory a path to directory on your server, run the code and post the output:

Code: Select all

echo "<form method = 'post' action = 'dir.php'>";
echo "<input type = 'text' name = 'directory'>";
echo "<input type = 'submit' value = 'Get contents'>";
echo "</form>";

$directory = $_POST["directory"];
$directory = "/path/to/directory";
$handle = opendir($directory);

$i = 0;
while ($file = readdir($handle)) {
  if ($i%2 == 1) {
    if (is_dir($file)) {
      echo "<b><font color = 'green'>", $file, "</font></b><br />
";
    } else {
      echo "<b>", $file, "</b><br />
";
    }
  }
    else {
    if (is_dir($file)) {
      echo "<font color = 'green'>", $file, "</font><br />
";
    } else {
      echo $file, "<br>";
    }
}
$i++;
}
closedir($handle);
Now, if you print the form and the list in the same file, you need to check that $_POST["directory"] is set (you always need to check it but here it's for sure):

Code: Select all

// Print the form...

if(isset($_POST["directory"]))
{
// List directory...
}
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

I found the problem!!!

is_dir() checks the dir relative to the script directory. Hence, if the script isn't running in the same directory as the user asked, is_dir will return false. Therefore, you need to add the directory from the form to the argument you pass to is_dir:

Code: Select all

$directory = $_POST["directory"];
$handle = opendir($directory);

$i = 0;
while ($file = readdir($handle)) {
  	if ($i%2 == 1) {
    		if (is_dir($directory."/".$file)) {
      			echo "<b><font color = \"green\">", $file, "</font></b><br />";
    		} else {
      			echo "<b>", $file, "</b><br />";
    		}
  	} else {
    		if (is_dir($directory."/".$file)) {
      			echo "<font color = 'green'>", $file, "</font><br />";
    		} else {
      			echo $file, "<br>";
    		}
	}
	$i++;
}
closedir($handle);
Note: readdir() returns the relative file/dir name to the directory you specified in opendir().
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Even if I copy and paste your code there's no difference in the results shown. The modulus works fine but there's as much green in the results as there is in The Sahara.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

The last code should work!
Try it again, but this time specify the directory:

Code: Select all

$directory = "/path/to/directory";
(right below $directory = $_POST['directory']; ).
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

With this very simple code it still doesn't output any directorys as green.

Code: Select all

while ($file = readdir($handle)) {
  if (is_dir($file)) {
    echo "<font color = 'green'>", $file, "</font><br>";
  } else {
    echo $file, "<br>";
}
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I used this one and it works for me

Code: Select all

<?php
echo '<form method="post" action="' . basename($_SERVER['SCRIPT_FILENAME']) . '">';
echo '<input type="text" name="directory">';
echo '<input type="submit" value="Get contents">';
echo '</form>';

if (isset($_POST['directory']))
{
    $directory = $_POST["directory"];
    
    $handle = opendir($directory);
    
    $i = 0;
    while ($file = readdir($handle)) 
    {
        if ($i%2 == 1) 
        {
            if (is_dir($file)) 
            {
                echo '<b><font color="green">' . $file . '</font></b><br />';
            } 
            else 
            {
                echo '<b>' . $file . '</b><br />';
            }
        }
        else 
        {
            if (is_dir($file)) 
            {
                echo '<font color="green">' . $file . '</font><br />';
            } 
            else 
            {
                echo $file . '<br>';
            }
        }
        $i++;
    }
    closedir($handle);
}
?>
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

You didn't read my post where I explained about is_dir(): viewtopic.php?p=337032#337032

EDIT:
everah: it works only when you read the directory where the script is in.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

ok wrote:The last code should work!
Try it again, but this time specify the directory:

Code: Select all

$directory = "/path/to/directory";
(right below $directory = $_POST['directory']; ).
Not working. It knows the directory, it knows it so well it's able to display what's in the directory. But it wont display a directory within a directory as green.

It doesn't any code with in

Code: Select all

if (is_dir($file))
but displays directories within the else statement. I'm 99% certain it's is_dir that's causing me problems.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Again...

Try this code:

Code: Select all

echo '<form method="post" action="' . basename($_SERVER['SCRIPT_FILENAME']) . '">';
echo '<input type="text" name="directory">';
echo '<input type="submit" value="Get contents">';
echo '</form>';

if (isset($_POST['directory']))
{ 
$directory = $_POST["directory"];
$handle = opendir($directory);

$i = 0;
while ($file = readdir($handle)) {
        if ($i%2 == 1) {
            if (is_dir($directory."/".$file)) {
            echo "<b><font color = \"green\">", $file, "</font></b><br />";
            } else {
            echo "<b>", $file, "</b><br />";
            }
        } else {
            if (is_dir($directory."/".$file)) {
            echo "<font color = 'green'>", $file, "</font><br />";
            } else {
            echo $file, "<br>";
            }
        }
        $i++;
}
closedir($handle);
}
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

ok wrote:You didn't read my post where I explained about is_dir(): viewtopic.php?p=337032#337032

EDIT:
everah: it works only when you read the directory where the script is in.
He speaketh the truth. It does only work when displaying data from the directory the script is contained in.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This one reads a directory relative to your current directory if you pass it a relative path.

Code: Select all

<?php
echo '<form method="post" action="' . basename($_SERVER['SCRIPT_FILENAME']) . '">';
echo '<input type="text" name="directory">';
echo '<input type="submit" value="Get contents">';
echo '</form>';

if (isset($_POST['directory']))
{
    $directory = $_POST["directory"];
    
    $path = realpath($directory);
    echo $path;
    
    $handle = opendir($directory);
    
    $i = 0;
    while ($file = readdir($handle)) 
    {
        $filetype = filetype($path . '/' . $file);
        
        if ($i%2 == 1) 
        {
            if ($filetype == 'dir') 
            {
                echo '<b><font color="green">' . $file . '</font></b><br />';
            } 
            else 
            {
                echo '<b>' . $file . '</b><br />';
            }
        }
        else 
        {
            if ($filetype == 'dir') 
            {
                echo '<font color="green">' . $file . '</font><br />';
            } 
            else 
            {
                echo $file . '<br>';
            }
        }
        $i++;
    }
    closedir($handle);
}
?>
Use it by passing either './' or './../' or some other path to a dir to it.
Post Reply