Directory of files not in alphabetical order

This is a discussion on "Directory of files not in alphabetical order" within the PHP Forum section. This forum, and the thread "Directory of files not in alphabetical order are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Dec 15th, 2005, 20:34
New Member
Join Date: Dec 2005
Location: Reading UK
Age: 38
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Question Directory of files not in alphabetical order

Using the following loop to run through a fileset on my server and present a set of links to those files for download.

PHP: Select all

<?php
        
echo "<p>";
        
// Define the full path to your folder from root
        
$path1 "songs/";
        
// Open the folder
        
$dir_handle = @opendir($path1) or die("Unable to open $path1");
        
// Loop through the files
        
while ($file readdir($dir_handle)) {
        if(
$file == "." || $file == ".." || $file == "index.php)
            continue;
            echo 
"<a href=\"".$path1.$file."\">".$file."</a><br />\n";
        }
        
// Close
        
closedir($dir_handle);
        echo 
"</p>"
    
?>
The loop is working fine but it shows the files in what appears to be no particaulr order - can anyone advise how i can make them appear in order?

Thanks,

Rob.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Dec 15th, 2005, 20:43
Rob's Avatar
Rob Rob is online now
Webforumz Founder
Join Date: Jul 2003
Location: Southern UK
Age: 34
Posts: 3,186
Blog Entries: 7
Thanks: 27
Thanked 23 Times in 20 Posts
Re: Directory of files not in alphabetical order

Not knowing PHP too well I think there will be a better way, but I personally would the filenames into an array and then sort it alphabetically.... then just loop through the array to pull out the filenames.
__________________
Click the 'Thanks!' button if this post has helped you

Rob - Webforumz Founder
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Dec 15th, 2005, 20:54
New Member
Join Date: Dec 2005
Location: Reading UK
Age: 38
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Directory of files not in alphabetical order

Thanks Rob.

I had though that but i wondered if there was something blindingly obvious that is houd do to get the effect.

Will prepare an array versio njust in case there is no other way.

Ta,

Rob.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Dec 16th, 2005, 01:47
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Directory of files not in alphabetical order

Yay!
http://ca.php.net/manual/en/function.asort.php
*runs into a wall*

You can sort an array alphabetically with asort($array)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Dec 16th, 2005, 04:30
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Directory of files not in alphabetical order

PHP's readdir returns the names of the file system objects in a directory in the order that they're actually stored in the index tables on the disc.

Sorting can be a resource hungry operation and you don't always need to get the names back in any particular order. For example, if you're traversing a whole directory tree and looking for just half a dozen files, there's no point in sorting all the names in every directory you go through and it would slow things right down (ever noticed how an ls or dir - which are the OS commands that DO produce sorted lists - pause noticably on very large directories?).

So - use readdir to traverse your directory/ies, store any file system objects that match your requirements into an array, and sort just that smaller list of wanted objects ...., yes, Phoenix, asort is one of the possible sort functions.

As an aside - do you want to sort your listing with all the upper case names ahead of the lower case names, or with them all folded together, or even by file size? Using readdir and something like sort, asort, ksort or natsort you have all the flexibility you could wish for.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Dec 16th, 2005, 09:34
New Member
Join Date: Dec 2005
Location: Reading UK
Age: 38
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Directory of files not in alphabetical order

So my code shoudl change to not display the items straight away but to put them in an array.

Then asort...

Then walkthrough the array and dsiplay all values using the fomat i've used already.

Cool and thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
directory, files, alphabetical, order

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Display text files in order cedric813 PHP Forum 2 Oct 22nd, 2007 13:48
Order By Maverick25r PHP Forum 2 Jun 28th, 2007 13:29
help with order for needed bruno89 Web Page Design 21 Oct 1st, 2006 04:38
Putting files in a directory franknu PHP Forum 1 Jun 27th, 2006 09:53
Tab Order aje Web Page Design 2 May 13th, 2006 14:49


All times are GMT. The time now is 15:49.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42