Page 1 of 1

is_dir not recognising directories

Posted: Thu Dec 07, 2006 11:05 am
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,

Posted: Thu Dec 07, 2006 11:20 am
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);

Posted: Thu Dec 07, 2006 11:26 am
by impulse()
Is still doesn't display any green.

If I put "echo 'foo'" within the is_dir condition it never displays it.

Posted: Thu Dec 07, 2006 11:41 am
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...
}

Posted: Thu Dec 07, 2006 11:56 am
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().

Posted: Thu Dec 07, 2006 11:58 am
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.

Posted: Thu Dec 07, 2006 12:04 pm
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']; ).

Posted: Thu Dec 07, 2006 12:05 pm
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>";
}
}

Posted: Thu Dec 07, 2006 12:06 pm
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);
}
?>

Posted: Thu Dec 07, 2006 12:06 pm
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.

Posted: Thu Dec 07, 2006 12:09 pm
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.

Posted: Thu Dec 07, 2006 12:11 pm
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);
}

Posted: Thu Dec 07, 2006 12:12 pm
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.

Posted: Thu Dec 07, 2006 12:13 pm
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.