Solved

glob() sorting

Posted on 2011-02-26
14
578 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mimic google as my ip 11 53
uploading image to a profile page 7 35
Update database from a form 8 35
What is the best PDF generator to use? 1 27
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

912 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now