I'm using this to get all images with a specific wildcard name:
<?php
$images = glob("/var/www/user/html/images/".$row['id']."@*.jpg");
foreach($images as $image) {
echo "<img src=\"".str_replace("/var/www/user/html/images/", "http://www.example.com/images/", $image)."\">
";
?>
If there are e.g. four images matching the pattern in glob() output is:
<img src="http://www.example.com/images/543@1.jpg">
<img src="http://www.example.com/images/543@2.jpg">
<img src="http://www.example.com/images/543@3.jpg">
<img src="http://www.example.com/images/543@4.jpg">
They're ordered correctly: 543@1.jpg, 543@2.jpg, 543@3.jpg, 543@4.jpg
.
But if there are e.g. 12 images output is like this:
<img src="http://www.exapmple.com/images/543@10.jpg">
<img src="http://www.exapmple.com/images/543@11.jpg">
<img src="http://www.exapmple.com/images/543@12.jpg">
<img src="http://www.exapmple.com/images/543@1.jpg">
<img src="http://www.exapmple.com/images/543@2.jpg">
<img src="http://www.exapmple.com/images/543@3.jpg">
<img src="http://www.exapmple.com/images/543@4.jpg">
<img src="http://www.exapmple.com/images/543@5.jpg">
<img src="http://www.exapmple.com/images/543@6.jpg">
<img src="http://www.exapmple.com/images/543@7.jpg">
<img src="http://www.exapmple.com/images/543@8.jpg">
<img src="http://www.exapmple.com/images/543@9.jpg">
As you see it isn't ordered correctly: 543@10.jpg, 543@11.jpg, 543@12.jpg, 543@1.jpg, 543@2.jpg, 543@3.jpg, […]
What can I do to fix it? Any ideas?
you can sort the array first then you can iterate it. use sort()
$images = glob("/var/www/user/html/images/".$row['id']."@*.jpg");
sort($images,2); // 2 for sort as string
foreach($images as $image) {
echo "<img src=\"".str_replace("/var/www/user/html/images/", "http://www.example.com/images/", $image)."\">
";
}