arcanine Posted July 27, 2008 Share Posted July 27, 2008 Okay he is my script that I've cobbled together, it used to work fine in till I changed server it's purpose it to read a directory (in this case /img) decide if images are in that folder and then output the file names in xml format now this worked fine in till I moved server when the xml file started displaying the image names non alphabetically (I can't have this happening I need my images in order, I have a lot of them and to rename them all would be impractical) this is where I need your help have a look through the code and help me re-write my code so that it outputs image file names alphabetically again <?php //define the xml standard header("Content-type: text/xml"); echo "<?xml version='1.0' encoding='ISO-8859-1'?>"; //this section calculates the image names in the directory $imgdir = 'img/'; // the directory, where your images are stored $allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show $dimg = opendir($imgdir); echo "\n<gallery> <image>\n"; while($imgfile = readdir($dimg)) { if(in_array(strtolower(substr($imgfile,-3)),$allowed_types)) { echo ' <url>img/' . $imgfile . "</url>\n"; } } echo ' </image> </gallery>'; ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 27, 2008 Share Posted July 27, 2008 You could read the file names into an array before sort()ing it. Quote Link to comment Share on other sites More sharing options...
arcanine Posted July 27, 2008 Author Share Posted July 27, 2008 hmm yeah I was just thinking that can you help me construct the new array (I'm still relatively new to php) we've got the imgfile string reading all the files so we need to feed those values into our new array so $imgfile = array (); sort($imgfile); Would work? I don't know if php will automatically fill my array with the strings also I guess I would need to convert the array back to strings to echo it out properly? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 27, 2008 Share Posted July 27, 2008 Try this: <?php //define the xml standard header("Content-type: text/xml"); echo "<?xml version='1.0' encoding='ISO-8859-1'?>"; //this section calculates the image names in the directory $imgdir = 'img/'; // the directory, where your images are stored $allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show $dimg = opendir($imgdir); echo "\n<gallery> <image>\n"; $imgs = array();//create the blank array while($imgfile = readdir($dimg)) { if(in_array(strtolower(substr($imgfile,-3)),$allowed_types)) { $imgs[] = $imgfile;//add to the array in the loop } } sort($imgs); foreach($imgs as $img){//loop through the images to output them echo ' <url>img/' . $img . "</url>\n"; } echo ' </image> </gallery>'; ?> Quote Link to comment Share on other sites More sharing options...
arcanine Posted July 27, 2008 Author Share Posted July 27, 2008 excellent my list is ordered thanks I'll be sure to have a look through the code and try understand the witchcraft behind it Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 27, 2008 Share Posted July 27, 2008 No worries. If you read the comments, you should follow. If you're all done, can you hit the topic solved button please? 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.