7

i have a little php script that reads a directory and then echos all the files (jpg's in this case) into a jquery image slider. it works perfectly, but i dont know how to sort the images by name desending. at the moment the images are random.

<?php
$dir = 'images/demo/';
if ($handle = opendir($dir)) {

while (false !== ($file = readdir($handle))) {
    echo '<img src="'.$dir.$file.'"/>';
}

closedir($handle);
}
?>

any help on this would be great.

one more thing i dont understand. the script pics up 2 nameless non jpg files in that folder that does not exist??? but i havnt realy checked into that yet

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
Piet Bez
  • 95
  • 1
  • 2
  • 7
  • 1
    *"then echos all the files (jpg's in this case)"* Given that `$dir = 'images/demo/';` assume you only have `.jpg` files in there. If not, it will show every file inside it. You could use `foreach (glob("folder/*.jpg") as $dir) {` instead. – Funk Forty Niner Aug 07 '13 at 18:27
  • @OP (Piet Bez) If you feel that anyone has answered your question and it has been resolved, you may accept one so that it will be marked as solved. Otherwise, it will remain "unanswered". Cheers (*Peace*) – Funk Forty Niner Aug 07 '13 at 18:55

3 Answers3

15

Try this:

$dir = 'images/demo/';
$files = scandir($dir);
rsort($files);
foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
        echo '<img src="' . $dir . $file . '"/>';
    }
}
Manoj Yadav
  • 6,560
  • 1
  • 23
  • 21
  • This `OR` should be an `AND` – Hanky Panky Aug 07 '13 at 18:25
  • thanks, this works perfectly. im sure the other suggestions would work too. this one just looked the least complicated. just one question. i dont understand the sorting part. at the moment it sorts the images asending, how could i sort them so it is desending? i need the highest value 10.jpg to be first and 1.jpg to be last. – Piet Bez Aug 07 '13 at 18:49
  • By default it's ascending you can use `rsort` for descending, Answer updated – Manoj Yadav Aug 07 '13 at 19:01
  • @ManojYadav Good job Manoj! Cheers – Funk Forty Niner Aug 07 '13 at 19:24
  • @PietBez Manoj did a good job, yet I must point out to make sure you only have `.jpg` files in your folder. If you have an "index" file or any other files that are not images, will show up as a broken image. Just thought you should be aware of that. Cheers (P.s.: I'm not taking away from Manoj's answer, it's a good one) :) (*Peace*) – Funk Forty Niner Aug 07 '13 at 19:26
5

Try putting each item in an array and then sorting that:

$images = array();

while (false !== ($file = readdir($handle))) {
    $images[] = $file;
}

natcasesort($images);

foreach ($images as $file) {
    echo '<img src="'.$dir.$file.'"/>';
}
abeanders
  • 51
  • 1
1

EDIT:

sorting version using the asort() function.

asort() ascending order - arsort() reverse order

<?php

// You can use the desired folder to check and comment the others.
// foreach (glob("../downloads/*") as $path) { // lists all files in sub-folder called "downloads"
foreach (glob("images/*.jpg") as $path) { // lists all files in folder called "test"

    $docs[$path] = filectime($path);
} arsort($docs); // sort by value, preserving keys

foreach ($docs as $path => $timestamp) {

// additional options
//    print date("d M. Y: ", $timestamp);
//    print '<a href="'. $path .'">'. basename($path) .'</a>' . " Size: " . filesize($path) .'<br />';

echo '<img src="'.$path.$file.'"/><br />'; 
}
?>


Previous answer

use the glob( ) function.

Making use of the glob() function, you can set the files and folder to your liking.

More on the glob( ) function on PHP.net

To display ALL files, use (glob("folder/*.*")

<?php

foreach (glob("images/*.jpg") as $file) { //change "images" to your folder
    if ($file != '.' || $file != '..') {
    
// display images one beside each other.
// echo '<img src="'.$dir.$file.'"/>';

// display images one underneath each other.
    echo '<img src="'.$dir.$file.'"/><br />'; 

    }
}
?>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141