I'm writing a routine to build an array of filenames from a directory. Here's the code:
<?php
//call line: <script type="text/javascript" src="a/build_array.php?folder=b&array=c&usefilenames=d"></script>
// where a=relative location of the php script file
// b=relative location of graphics folder
// c=name of array to be built
// d=use file names as captions:yes|no
Header("content-type: application/x-javascript");
function returnimages($arrayname) {
$dirname="../" . $_GET["folder"] . "/";
$pathname="" . $_GET["folder"] . "/";
$pattern="\.(jpg|jpeg|png|gif|bmp)$";
$curimage=0;
$str=',""';
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){
if($_GET["usefilenames"]=='yes')
$str = ', "' . preg_replace('/\.[^\.]*$/', '', $file) . '"';
echo $arrayname . '[' . $curimage .']=["' . $pathname . $file . '", "", ""' . $str . '];' . "\n";
$curimage++;
}
}
closedir($handle);
}
}
$arrayname=$_GET["array"];
echo 'var ' . $arrayname . '=new Array();' . "\n";
returnimages($arrayname);
?>
The problem is that when I display the files using the array, they aren't in alphabetical order. I've tried adding a sort function [i.e. sort($arrayname);] but it doesn't work.
Can anyone tell me how to do the sort?
I should note that the file names are both alphabetic (i.e. Dscf0050.jpg) and numeric (i.e. 100-01234.jpg).
Thanks for your help.