The PHP returnimages function you are using simply reads the folder for files with the extensions jpg|jpeg|png|gif|bmp. It doesn't sort them. It takes them in the order in which the server gives them, which is usually the order in which they were uploaded. If you change that order by replacing one or more of them, or if the server is updated and that somehow changes the order or changes what the server looks for to establish the order, that will result in something like you've been experiencing. What Vic has done is have javascript sort them numerically. Now it no longer matters what the the order is on the server, as long as a numeric sort gets you the order you're looking for, which apparently it does. Makes sense since the image filenames have an obvious numeric order to them.