php read directory sorting
Asked Answered
D

3

8

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

Danille answered 7/8, 2013 at 18:15 Comment(2)
"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.Gap
@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)Gap
M
16

Try this:

$dir = 'images/demo/';
$files = scandir($dir);
rsort($files);
foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
        echo '<img src="' . $dir . $file . '"/>';
    }
}
Mordy answered 7/8, 2013 at 18:24 Comment(5)
This OR should be an ANDBegorra
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.Danille
By default it's ascending you can use rsort for descending, Answer updatedMordy
@ManojYadav Good job Manoj! CheersGap
@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)Gap
L
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.'"/>';
}
Laconia answered 7/8, 2013 at 18:21 Comment(1)
Great, this is the most minimal invasive change to achieve sorting.Innovation
G
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 />'; 

    }
}
?>
Gap answered 7/8, 2013 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.