Windows vs. Linux Directory Sorting

I’m developing a site on a Windows host for a client whose site is hosted on Linux and just ran into an unexpected issue. My PHP code is supposed to display all of the images in a particular folder on a page. I renamed all of the images so they’d be read in the order I wanted them displayed. My code looked something like this:

$path = ‘images/thumbs’;
$folder = dir($path);
while($img=$folder->read()) {
if($img== “.” || $img== “..”) continue;
echo ‘
<div id=”imgPod”>

    <div id=”image”>

      <a href=”images/’.$img.'” <img src=”‘.”$path/$img”.'” /></a>

    </div>

</div>’;
}

The sorting worked like a charm on my host but when I published it live the images were scrambled. Turns out Linux doesn’t read the files in alphabetical order. What I had to do instead was to first read the filenames into an array, then sort the array and display the images in the order of the newly sorted array. The new code:

$path = ‘images/thumbs’;
$folder = dir($path);
$img_array = array();
while($img=$folder->read()) {
if($img== “.” || $img== “..”) continue;
$img_array[] = $img;
}

sort($img_array);
foreach ($img_array as $image) {echo ‘
<div id=”imgPod”>

    <div id=”image”>

      <a href=”images/’.$image.'” <img src=”‘.”$path/$image”.'” /></a>

    </div>

</div>’;
}

Not much extra coding and it worked great.