Page 1 of 1
is_dir() killing my loop
Posted: Sat Jan 24, 2004 8:49 pm
by php_seth
i'm a newb. take pity..
Code: Select all
<ul>
<?
$dir = opendir("../photos/");
while (false != ($file = readdir($dir))) {
if (is_dir($file) && $file !== '.' && $file !== '..') {
echo "<li><a href="../photos/$file" title="$file">$file</a></li>";
}
}
closedir($dir);
?>
</ul>
displays nothing while the same thing with the "is_dir($file)" condition removed from that if statement displays a list of the contents of the folder, dirs and files alike.
Posted: Sun Jan 25, 2004 12:34 am
by Gen-ik
Try this...
Code: Select all
<?php
$dir = opendir("../photos/");
while (false !== ($file = readdir($dir)))
{
if(is_dir($file) && $file != "." && $file != "..")
{
echo "<li><a href="../photos/$file" title="$file">$file</a></li>";
}
}
closedir($dir);
?>
I take it you're trying to echo() a list of folders?
If your trying to echo() image files then change
is_dir($file) to
!is_dir($file) and remove the
&& $file != "." && $file != ".." bit.
Posted: Sun Jan 25, 2004 1:08 am
by php_seth
that isn't working either.

i don't understand why though.
Posted: Sun Jan 25, 2004 2:41 am
by markl999
With is_dir($file) .. $file is ONLY a filename, it has no path part.
So you probably want to be checking is_dir('../photos/'.$file)
Posted: Sun Jan 25, 2004 3:18 am
by php_seth
yeah, i tried that too, and still nothing.
gah!
Posted: Sun Jan 25, 2004 8:23 am
by Gen-ik
I was a wee bit tired last time I replied and I completely overlooked something. Try this...
Code: Select all
<?php
$dir = opendir("../photos"); // Removed trailing forward slash
while (false !== ($file = $dir->read())) // made a change here
{
if(is_dir($file) && $file != "." && $file != "..")
{
echo "<li><a href="../photos/$file" title="$file">$file</a></li>";
}
}
$dir->close(); // made a change here
?>
Posted: Sun Jan 25, 2004 8:39 pm
by php_seth
what does -> mean?
Posted: Sun Jan 25, 2004 8:42 pm
by php_seth
Fatal error: Call to a member function on a non-object in /home/sethras/public_html/v2/_port_photo.php on line 17
Code: Select all
while (false !== ($file = $dir->read())) // made a change here
Posted: Mon Jan 26, 2004 8:55 am
by Gen-ik
You are creating an OBJECT when you set the directory using opendir() and the -> will be calling a variable or function from that OBJECT.
:: [php_man]dir[/php_man]