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
impulse()
Forum Regular
Posts: 748 Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:
Post
by impulse() » Thu Dec 07, 2006 11:05 am
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,
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Thu Dec 07, 2006 11:20 am
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() » Thu Dec 07, 2006 11:26 am
Is still doesn't display any green.
If I put "echo 'foo'" within the is_dir condition it never displays it.
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Thu Dec 07, 2006 11:41 am
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...
}
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Thu Dec 07, 2006 11:56 am
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() » Thu Dec 07, 2006 11:58 am
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.
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Thu Dec 07, 2006 12:04 pm
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() » Thu Dec 07, 2006 12:05 pm
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>";
}
}
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Thu Dec 07, 2006 12:06 pm
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);
}
?>
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Thu Dec 07, 2006 12:06 pm
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() » Thu Dec 07, 2006 12:09 pm
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
but displays directories within the
else statement. I'm 99% certain it's
is_dir that's causing me problems.
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Thu Dec 07, 2006 12:11 pm
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() » Thu Dec 07, 2006 12:12 pm
He speaketh the truth. It does only work when displaying data from the directory the script is contained in.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Thu Dec 07, 2006 12:13 pm
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.