Forum Moderators: coopster

Message Too Old, No Replies

get first image in folder

         

sneaks

6:54 pm on Oct 3, 2005 (gmt 0)

10+ Year Member




function getFirstImage($dirname)
{
global $imageName;
$ext = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "GIF", "JPEG");
if($handle = opendir($dirname))
{
while(false!== ($file = readdir($handle)))
{
if(strstr($file, "." . $ext[$i]))
{
$imageName = $file;
break;
}
}
closedir($handle);
}
return($imageName);
}

just trying to get the name if the first image in the spacified folder...

thanks for any help!

jatar_k

7:02 pm on Oct 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what does it not do or how is it misbehaving?

as an aside instead of strstr you could use the case insensitive version so you don't have to have both upper and lower case extensions in your comparison array and it would then handle mixed case extensions

[php.net...]

sneaks

7:24 pm on Oct 3, 2005 (gmt 0)

10+ Year Member



$imageName = '.'

j.

sneaks

7:33 pm on Oct 3, 2005 (gmt 0)

10+ Year Member



man, i always do this...
here it is...


function getFirstImage($dirname)
{
global $imageName;
$ext = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "GIF", "JPEG");
if($handle = opendir($dirname))
{
while(false!== ($file = readdir($handle)))
{
if(strstr($file, "." . $ext[$i])!= '.' && strstr($file, "." . $ext[$i])!= '..')
{
break;
}
}
$imageName = $file;
closedir($handle);
}
return($imageName);
}

that way is solid cause then if returns '..' you know it has no images... will lookinto that stristr function

thanks,
jonathan