Forum Moderators: coopster

Message Too Old, No Replies

Limit directory Output

how to make my code only display jpeg, png and gif files?

         

bysonary

8:24 pm on Jan 28, 2007 (gmt 0)

10+ Year Member


Hello, below is the code I am using to display a list of the filenames from a directory, I want to be able to make this script only show image files, at the moment it displays everything including the directories in the directories. here is the code.

<?
$dir = "/home/www/juttuffi/gallery/images";
$d = dir($dir);

while($entry = $d->read())
{
if ($entry!= "." && $entry!= ".."[smilestopper])
{
echo "<option>".$entry."</option>";
}
}
$d->close();
?>

can anyone help me out, cheers!

cameraman

10:47 pm on Jan 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's a good example at [us3.php.net ]

bysonary

11:06 pm on Jan 28, 2007 (gmt 0)

10+ Year Member



what so.....

<?
$dir = "/home/www/juttuffi/gallery/images";
$d = dir($dir);
$type = array('jpeg', 'jpg', 'gif', 'bmp', 'png');


while($entry = $d->read())
{
if ($entry!= "." && $entry!= ".." && array_search($type)!== false)
{
echo "<option>".$entry."</option>";
}
}
$d->close();
?>

that would work?

bysonary

11:16 pm on Jan 28, 2007 (gmt 0)

10+ Year Member


that doesnt work and I am no further on in terms of understanding how to display only image files, I would really like to get this to work with my existing code that lists the directory as it easilly integrates into a HTML form, any pointers from anyone else?

jatar_k

3:48 am on Jan 29, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what is it that doesn't work?

a test for extension is what you are looking for unless you want to get into more complicated tests about whether it is actually an image regardless of extension.

bysonary

10:56 am on Jan 29, 2007 (gmt 0)

10+ Year Member


Just a simple test for extension is what I need the code above with the array_search and the $type array added still lists directories and php files as well as all other types of file where I only want to list jpeg, gif, bmp and png files.

eelixduppy

12:32 pm on Jan 29, 2007 (gmt 0)



Try something like this:

$dir = "path/to/images";
$d = [url=http://us2.php.net/manual/en/function.opendir.php]opendir[/url]($dir);
$type = array(IMAGETYPE_GIF,IMAGETYPE_PNG,IMAGETYPE_JPEG,IMAGETYPE_BMP);

echo '<select name="images">';
while($entry = [url=http://us2.php.net/manual/en/function.readdir.php]readdir[/url]($d))
{
if ($entry!= "." && $entry!= ".." && [url=http://us2.php.net/manual/en/function.array-search.php]array_search[/url]([url=http://us2.php.net/manual/en/function.exif-imagetype.php]exif_imagetype[/url]($dir."/".$entry),$type)!== false)
{
echo "<option>".$entry."</option>";
}
}
echo '</select>';

bysonary

1:23 pm on Jan 29, 2007 (gmt 0)

10+ Year Member



you legend, it works just how I want, thank you very much!