Page 1 of 1

php reading directory problem

Posted: Tue Sep 19, 2006 5:03 pm
by satheshf12000
hi all..

I am reading all image files by this code.

Code: Select all

$dir = "images/";
$jpgext = ".jpg";
$dh = opendir($dir);

while($filename = readdir($dh))
{
$filepath = $dir.$filename;
if(is_file($filepath) and ereg("\.jpg$",$filename))
{
$gallery[] = $filepath;
$fnarray[] = $filename;
$count++;
}
}
the problem is i am not getting the file names sorted in fnarray[]. when i print it i get like 50.jpg then 35.jpg then 1.jpg then 32.jpg then 26.jpg.. etc but i need to get like 1.jpg, 2.jpg, 3.jpg.. etc.. in ascending order while reading.. What should i do ?

Posted: Tue Sep 19, 2006 5:09 pm
by Luke
1. look into using glob()
2. while looping through the directory, store the files in an array
3. refer to this thread: viewtopic.php?t=55383&highlight=usort

Posted: Tue Sep 19, 2006 5:50 pm
by daedalus__
glob() is insanely slow.

Try using a directory handlers, a loop, and array sorting functions.

Posted: Tue Sep 19, 2006 5:51 pm
by feyd
Why, pray tell, is this in Client Side?