is_dir() killing my loop

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
php_seth
Forum Newbie
Posts: 5
Joined: Sat Jan 24, 2004 8:49 pm
Location: Minneapolis, MN

is_dir() killing my loop

Post by php_seth »

i'm a newb. take pity..

Code: Select all

<ul>
<?
$dir = opendir("../photos/");
while (false != ($file = readdir($dir))) &#123;
		if (is_dir($file) && $file !== '.' && $file !== '..') &#123;
			echo "<li><a href="../photos/$file" title="$file">$file</a></li>";
		&#125;
&#125;
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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
php_seth
Forum Newbie
Posts: 5
Joined: Sat Jan 24, 2004 8:49 pm
Location: Minneapolis, MN

Post by php_seth »

that isn't working either. :( i don't understand why though.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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)
php_seth
Forum Newbie
Posts: 5
Joined: Sat Jan 24, 2004 8:49 pm
Location: Minneapolis, MN

Post by php_seth »

yeah, i tried that too, and still nothing. :(

gah!
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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

?>
php_seth
Forum Newbie
Posts: 5
Joined: Sat Jan 24, 2004 8:49 pm
Location: Minneapolis, MN

Post by php_seth »

what does -> mean?
php_seth
Forum Newbie
Posts: 5
Joined: Sat Jan 24, 2004 8:49 pm
Location: Minneapolis, MN

Post 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
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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]
Post Reply