ricky-spires Posted April 16, 2013 Share Posted April 16, 2013 hello. im having some trouble getting the information i want back from a folder. all i want to get back is the file names in numeric order. i seem to also be getting directories listed. my files are called 1.jpg, 2.jpg, 3.jpg etc up to 50.jpg i have added the work page in the echo. at the moment i get this: Page Page Page 1 Page 10 Page 11 Page 12 Page 13 Page 14 Page 15 Page 16 Page 17 Page 18 Page 19 Page 2 Page 20 Page 21 so i want to get rid of the first 2 and put the rest in order . this is my code: <?php if ($pdfImage = opendir('assets/images/pages/')) { while (false !== ($pageImage = readdir($pdfImage))) { $pageImage = preg_replace('/\D/', '', $pageImage); echo'<li><a href="'.$pageImage.'">Page '.$pageImage.'</a></li>'; } closedir($pdfImage); } ?> it doesnt have to be opendir. if you know another way i would be happy to hear . thanks ricky Quote Link to comment Share on other sites More sharing options...
Barand Posted April 16, 2013 Share Posted April 16, 2013 (edited) store in an array then sort the array using natsort Edited April 16, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 16, 2013 Share Posted April 16, 2013 (edited) A simply way would be to use glob(): $files = glob('assets/images/pages/*.jpg'); sort($files); Edit: Er, yes, natsort() solves your other problem. I missed that. Good call, Barand! Edited April 16, 2013 by lemmin Quote Link to comment Share on other sites More sharing options...
ricky-spires Posted April 17, 2013 Author Share Posted April 17, 2013 great. thanks this worked: <?php $files = glob('assets/images/pages/*.jpg'); $pageImg = preg_replace('/\D/', '', $files); natsort($pageImg); foreach($pageImg as $file){ echo'<li><a href="'.$file.'">Page '.$file.'</a></li>'; } ?> 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.