$mydir = dir('gallery/tn/');
while(($file = $mydir->read()) !== false) {
if ($file == "."|| $file == ".."|| $file == "Thumbs.db"){}
else {
echo ("<a href='gallery.php?pic=$file'><img border='0' src='gallery/tn/$file' alt='' /></a>");
}
}
$mydir->close();
reading from a directory
Started by rik, Mar 04 2006 01:19 PM
4 replies to this topic
#1
Posted 04 March 2006 - 01:19 PM
im using this code to read from a directory and display images from it, i just want to know if theres any way i can make it display the images in a different order, it defaults to alphabetical order, but id like to change it, anyone know how?
#2
Posted 04 March 2006 - 04:41 PM
What type of order are you looking for? The default way is alphabetical order of course, that is because thats how the server reads any directory.
#3
Posted 04 March 2006 - 05:27 PM
if possible id like a 'last modified' order, though im not sure if that is possible. when i add pictures to the directory id like them to be displayed first.
i suppose i could just name them in a chronological order, but thats no fun!
edit: ok say i have all the pictures named 1.jpg, 2.jpg etc how would i get it to display them with the biggest number first i.e. 10.jpg, 9,jpg,8.jpg and so on
i suppose i could just name them in a chronological order, but thats no fun!
edit: ok say i have all the pictures named 1.jpg, 2.jpg etc how would i get it to display them with the biggest number first i.e. 10.jpg, 9,jpg,8.jpg and so on
Edited by rik, 04 March 2006 - 06:14 PM.
#4
Posted 05 March 2006 - 01:52 AM
You could probably create a function to sort the files from 10 =< 1 If you search 'sorting files' on php.net you get a whole list of results and some code snippets other than that I'm not sure.. I'll write some code see what I can do
OK..
This is what I got
I put 6 text files in a folder called count but it repeated the first 3 2x each so if anyone can find the bug post it 
P.S. I hoped this helped if not then sorry !
OK..
This is what I got
<?php
$myfile = dir('../NonBlog/count/');
$files = array();
$i = 0;
while (($file = $myfile->read()) != false) {
if ($file == '.' || $file == '..') {}
else {
$files[$i] = $file;
rsort($files);
$filecount = count($files);
while($i < $filecount) {
echo '../' . $files[$i] . '<br />';
$i++;
}
}
}
$myfile->close();
?>
Unfortunatley The Output I got wasQuote
../3.txt
../3.txt
../2.txt
../2.txt
../1.txt
../1.txt
../3.txt
../2.txt
../2.txt
../1.txt
../1.txt
P.S. I hoped this helped if not then sorry !
Edited by Hit3k, 05 March 2006 - 04:16 AM.
#5
Posted 05 March 2006 - 02:46 PM
while (($file = $myfile->read()) != false) {
if ($file != '.' || $file != '..'){
$files[$i] = $file;
}
}
$filecount = count($files);
while($i < $filecount) {
echo $files[$i] . '<br />';
$i++;
}
With any luck, that should work a little better. You should note how i removed the else statement since it will make things look a little cleaner.And, if you want to sort by the modification time, i would look into the filemtime function, and change the above script to work with that.
note: To get the sort to work right, i would store the modification times of the files into an array, with the filename as the key.
i.e.
$list[filemtime($file[$i]] = $file[$i];You can refer to php.net for more info on the thousands of sort functions they have (the correct one for this would be asort() i believe).
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
