Forum Moderators: coopster

Message Too Old, No Replies

List files in a directory

Need to remove '.' and '..'

         

mooger35

6:54 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



I have a slide show gallery that I want to make a little easier to update. What I would like is the files in my thumbs directory to be pulled automatically into the script.

This is the php code I found on this site but unfortunately the '.' and '..' are still causing problems. What I'd like is for them to not appear at all so the first row is my first file. Any help?

$imgdir = "images/gallery/thumbs/";
echo "<table border='1'>";
$img=opendir($imgdir);
while($imgb=readdir($img))
{
$imgb = str_replace(".","",$imgb);
$imgb = str_replace("..","",$imgb);
$imgb = str_replace("jpg",".jpg",$imgb);
echo "<tr><td>$imgb</td></tr>";
}
closedir($img);
echo "</table>";

dislpays:
. //str_replace removes and leaves as ""
.. //str_replace removes and leaves as ""
041010a.jpg
041010b.jpg
041010c.jpg
041010d.jpg
041010e.jpg
041114a.jpg
etc etc etc...

dreamcatcher

7:29 pm on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




while($imgb=readdir($img))
{
if ($imgb!='.' && $imgb!='..')
{
echo "<tr><td>$imgb</td></tr>";
}
}

What you are now saying is if the variable does not equal . or .. list it. No need for str_replace here.

Hope that helps.

dc

mooger35

7:34 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



I've added "File:" to show my problem.

$imgdir = "images/gallery/thumbs/";
echo "<table border='1'>";
$img=opendir($imgdir);
while($imgb=readdir($img))
{
if ($read!='.' && $read!='..')
{
$imgb = str_replace(".","",$imgb);
$imgb = str_replace("..","",$imgb);
$imgb = str_replace("jpg",".jpg",$imgb);
echo "<tr><td>File: $imgb</td></tr>";
}
}
closedir($img);
echo "</table>";

Still gives me two blank rows:
File:
File:
File: 041010a.jpg
File: 041010b.jpg
File: 041010c.jpg
etc etc etc...

dreamcatcher

7:36 pm on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I updated my post. Got the variable names wrong. Check my previous post.

dc

mooger35

7:41 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



Perfect!

Thanks

dreamcatcher

7:42 pm on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You`re welcome. Again, sorry about that, I use $read that many times I typed it without thinking.

dc

mcibor

9:18 am on Nov 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can also use the continue (you're ini the loop)

while($imgb=readdir($img))
{
if ($imgb == '.' ¦¦ $imgb == '..') continue;

echo "<tr><td>$imgb</td></tr>";
}

Both are elegant solutions
Michal Cibor

directrix

9:55 am on Nov 22, 2005 (gmt 0)

10+ Year Member



You could also exclude . and .. by using the is_dir [php.net] function:

if (!is_dir($file)) {...

(See also is_file [php.net])