Jump to content

ordering my images


arcanine

Recommended Posts

 

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>';

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>';

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.