Arranging output of opendir function by last modification?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Sorry about the double post, but i think i have got it :D
!!!

But one last question... well i think it will be may last...

<?php

$arrayFiles=glob('*.*');
if($arrayFiles){
foreach ($arrayFiles as *WHAT_DO_I_PUT_HERE*) {
echo "$filename size <br>";
}
}
else
echo"File not found."

?>

:)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Almost there. You need to determine the modified time of every file returned by glob(), before you loop through it. You also need to asort() the results so you get them in chronological order:

Code: Select all

$myArray = array("file1"=>1,"file2"=>10,"file3"=>3);//assume 1,10 and 3 are file modification times
asort(myArray);
$myArray is now:
"file1"=>1,
"file3"=>3,
"file2"=>10

You can then loop through the resulting array.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Hoe exactly do i get the times into

Code: Select all

array("file1"=>1,"file2"=>10,"file3"=>3);/
Do i put the filemtime function in there?

Code: Select all

. date ("F d Y H:i:s.", filemtime($filename));
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Yep. But filemtime() will give you a timestamp - which you can sort. Formatting the date before you sort may not give you 100% accurate results.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Cool.

But my current script is:

Code: Select all

<?php

$arrayFiles=glob('*.*');
if($arrayFiles){
foreach ($arrayFiles as . date ("F d Y H:i:s.", filemtime($filename)); {
echo "$filename size <br>";
}
}
else
echo"File not found."

?>
and i get this error:

Code: Select all

Parse error: parse error, unexpected '.', expecting '&' or T_STRING or T_VARIABLE or '$' in /host/localhost/htdocs/dir/index.php on line 5
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Line 5 is the foreach(). 3 things wrong with that line:
  1. Don't put a period (.) between 'as' and 'date'
  2. You can't put a function in the foreach like that. Just pull out the $filename, then call filemtime on that
  3. Don't end that line with a semi-colon. A semi-colon means end-of-statement, and you're not done the foreach statement at that point.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

So would this be right:

Code: Select all

<?php

$arrayFiles=glob('*.*');
$date=date ("F d Y H:i:s.", filemtime($filename)
if($arrayFiles){
foreach ($arrayFiles as $date) {
echo "$filename size <br>";
}
}
else
echo"File not found."

?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Syntactically it looks ok. However, you're still missing the asort().

Read my previous comments - I've told you repeatedly what you need to do and in what order. Don't rely on me to tell you what you need to do - read the manual & the documentation for the functions and figure out what they do.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Just before i move on to the asort(), im having trouble with:
ozzy wrote:

Code: Select all

<?php

$arrayFiles=glob('*.*');
$date=date ("F d Y H:i:s.", filemtime($filename)
if($arrayFiles){
foreach ($arrayFiles as $date) {
echo "$filename size <br>";
}
}
else
echo"File not found."

?>
It is returning this error :

Code: Select all

Parse error: parse error, unexpected T_IF in /host/localhost/htdocs/dir/index.php on line 5
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Line 4 - you're missing a semi-colon.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Now im getting

Code: Select all

Parse error: parse error, unexpected ';' in /host/localhost/htdocs/dir/index.php on line 4
Last edited by ozzy on Wed Apr 12, 2006 4:53 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You've also got unbalanced parenthesis :roll:
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

How do i fix a unbalanced parenthesis?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Put an extra one in.

Look at line 4. Just look at it - it doesn't look right.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Code: Select all

<?php
$arrayFiles=glob('*.*');
$date=date ("F d Y H:i:s.", filemtime($filename));
if($arrayFiles){
  foreach ($arrayFiles as $filename) {
   echo "$filename <br>";
  }
}
else
  echo"File not found."

?>
It workes fine when i leave it as below

Code: Select all

foreach ($arrayFiles as $filename) {
Bust as soon as i change it to (see below) the files arnt displayed; the page is just blank apart from the <br>'s.

Code: Select all

foreach ($arrayFiles as $date) {
Post Reply