?
Solved

glob() sorting

Posted on 2011-02-26
14
Medium Priority
?
600 Views
Last Modified: 2012-05-11
I am trying to sort the files in a glob("files/*.*") import ... but Im not able to sort  the result by name ... anyone know how to do this?


Folder with text files:

2011.01.25 - Tue, 25 Jan 2011.txt
2011.01.26 - Wed, 26 Jan 2011.txt
2011.01.27 - Thu, 27 Jan 2011.txt
2011.01.28 - Fri, 28 Jan 2011.txt
0
Comment
Question by:Hans Inge Hagen
  • 7
  • 6
14 Comments
 
LVL 4

Expert Comment

by:DanDauchy
ID: 34989314
have you tried places each name in an array and then sorting the array w/ asort:

http://www.php.net/manual/en/function.asort.php
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34991976
If the names are as you have shown them and yur want them in increasing date sequence then this will do it

$arr = glob("files/*.*");
asort( $arr );
print_r( $arr );

0
 

Author Comment

by:Hans Inge Hagen
ID: 34992083
This is what I have:

$files = glob("content/$dir/*.*"); for ($i=0; $i<count($files); $i++) { $num = $files[$i];
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34995628
Try this...

$files = glob("content/$dir/*.*");

asort( $files );

for ($i=0; $i<count($files); $i++) {
    $num = $files[$i];
    ..... etc
0
 

Author Comment

by:Hans Inge Hagen
ID: 34995756
I did try that with no success.

Maybe, there are some way to sort the files serverside? .. a script or something?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34995813
What order are you trying to sort them in to? The example you gave above was already sorted in date order.
0
 

Author Comment

by:Hans Inge Hagen
ID: 34995853
It is sorted as:

2011.01.25 - Tue, 25 Jan 2011.txt
2011.01.26 - Wed, 26 Jan 2011.txt
2011.01.27 - Thu, 27 Jan 2011.txt
2011.01.28 - Fri, 28 Jan 2011.txt

I need it to be:

2011.01.28 - Fri, 28 Jan 2011.txt
2011.01.27 - Thu, 27 Jan 2011.txt
2011.01.26 - Wed, 26 Jan 2011.txt
2011.01.25 - Tue, 25 Jan 2011.txt

in the output. This is a diary where the latest text based file should be on top. 8)
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34995871
Use arsort instead of asort.
0
 

Author Comment

by:Hans Inge Hagen
ID: 34995930
It seems that I will need to make an Array of the $files to be able to asort/arsort the list ...
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 200 total points
ID: 34996026
"It seems that I will need to make an Array of the $files to be able to asort/arsort the list ..."

glob returns an array, that's why I coded it the way I did.
0
 

Author Comment

by:Hans Inge Hagen
ID: 34996043
When I "echo $files" the result writes  "Array" .. I dont know ..
0
 

Author Closing Comment

by:Hans Inge Hagen
ID: 34996081
Thanks
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34996149
You cannot echo an array. Use print_r instead

print_r( $files );

0
 

Author Comment

by:Hans Inge Hagen
ID: 34996223
Great. The files is now sortet correctly ... the problem might be in the file_get_content("$num");

source:

$files = glob("content/$dir/*.*");

arsort($files);
print_r( $files );

for ($i=0; $i<count($files); $i++) {
$num = $files[$i];

$str= file_get_contents("$num");
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question