Dustin013 Posted March 13, 2009 Share Posted March 13, 2009 So I have a script that is scanning a directory to grab the file names for use in a gallery. However, for some reason I can't get it to sort the files alphabetically. I have tried to comment the code to give you a good idea of what I am doing, and where I am thinking I am missing stuff. So, any suggestions would rock, but most importantly I can't figure out why not ordering the contents of the directory according to file names in alphabetical order. <?php function returnimages($dirname="images/gallery/images/refacing/") { $pattern="\.(jpg|jpeg|png|gif|bmp)$"; $files = array(); if($handle = opendir($dirname)) { while(false !== ($file = readdir($handle))){ if(eregi($pattern, $file)){ echo $file.":"; // Ok so I don't want an echo here, I was attempting to pass all the file contents to the variable $files that is returned with each filename separated by a ":" so that I can explode them. } } closedir($handle); } return($files); // this will return something like this (refacing-08.jpg:refacing-18.jpg:refacing-01.jpg:refacing-14.jpg:refacing-13.jpg:refacing-04.jpg) // Notice how the array is out of order. The file names are named blah01, blah02, etc... yet it randomly orders them. } // Now I call the function to output the files... this is where I am all jacked up, but nevertheless will give you an idea of what I am trying to do. // Start Gallery $filearr = returnimages(); // I was trying to place the contents of the $files array into $filearr but all it does it echo the files $filename = explode(":", $filearr); // Then I was trying to explode the array of filenames as $filename sort($filename); // Put the array into alphabetical order?? foreach($filename as $i => $value){ // For everyfile found, I wanted to output the following echo '<li><h3></h3><span>images/gallery/images/refacing/'.$i.'</span><p></p><a href=""><img src="images/gallery/images/refacing/thumbs/'.$i.'"> </a></li>'; } echo '</ul><div id="txtContainer" class="float-left">'; // End Start Gallery ?> Now the above really doesn't work, so I will show you the original code I have that works, yet doesn't put the file names in order... <?php $pageURL = 'window-gallery'; include('includes/global.php'); include('includes/page-info.php'); $glbl = new specialIncludes(); $ctrl = new mainControls(); echo $glbl->globalHeader($title, $metaDesc, $metaKeys); function returnimages($dirname="images/gallery/images/windows/") { echo '<ul id="slideshow">'; $pattern="\.(jpg|jpeg|png|gif|bmp)$"; $files = array(); if($handle = opendir($dirname)) { while(false !== ($file = readdir($handle))){ if(eregi($pattern, $file)){ echo ' <li><h3></h3><span>images/gallery/images/windows/'.$file.'</span><p></p><a href=""><img src="images/gallery/images/windows/thumbs/'.$file.'"></a></li> '; } } closedir($handle); } echo '</ul><div id="txtContainer" class="float-left">'; return($files); } // Start Gallery returnimages(); // End Start Gallery ?> I have been reading all morning and simply can't figure it out, so any suggestions would be helpful. Thanks again! Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 13, 2009 Share Posted March 13, 2009 first of all...your function isn't returning anything. you create an array, but don't add anything to it: <?php function returnimages($dirname="images/gallery/images/refacing/") { $pattern="\.(jpg|jpeg|png|gif|bmp)$"; $files = array(); if($handle = opendir($dirname)) { while(false !== ($file = readdir($handle))){ if(eregi($pattern, $file)){ $files[] = $file; } } closedir($handle); } return($files); // this will return an array of files } $filearr = returnimages(); //$filearr is now an array of files sort($filearr); // Put the array into alphabetical order foreach($filearr as $file){ // For every file found, I wanted to output the following echo '<li><h3></h3><span>images/gallery/images/refacing/'.$file.'</span><p></p><a href=""><img src="images/gallery/images/refacing/thumbs/'.$file.'"> </a></li>'; } echo '</ul><div id="txtContainer" class="float-left">'; // End Start Gallery ?> Quote Link to comment Share on other sites More sharing options...
Dustin013 Posted March 13, 2009 Author Share Posted March 13, 2009 Thanks, that did the trick! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.