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

Arranging output of opendir function by last modification?

Post by ozzy »

Hey,

How i would i arrange the output (files and folders) from the opendir function by the last modification (filemtime function)?

Eg. of the open dir function

Code: Select all

<?php
$dir = "./";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
       }
       closedir($dh);
   }
}
?>
Thanks in advance! :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

store them into an array with the file modification time. Sort the data.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Thanks, but im not to sure how i would put that together; im a newbie. :?
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 »

The quickest way I can think of would be to do a glob of the directory to get all the files. Then, loop through each filename and call filemtime() on each file. Then store the filename and modified time (as a UNIX timestamp) in an array. Sort the array to get the information in chronological order.
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 »

Im not too sure what you gusy meen, im not being lazy i just dont understand... but could oen of you guys write a quick something up using the basis of:

Code: Select all

<?php
$dir = "./";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
       }
       closedir($dh);
   }
}
?>
- abit cheaky of me
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

play with arrays for a while, you're figure it out.
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 »

glob() gives you an array of files, with the filenames being the values of the array
Loop through that resulting array and call filemtime() on each filename
After getting the modified time, make a new array with the keys being the filename, and the value being the timestamp
Call asort() on that 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 »

Where would i get the data for the glob?

Would i collect it from the index file where the directory is located?

So it would be like :

Code: Select all

<?php
foreach (glob("index.php") as $filename) {
   echo "$filename size " . filesize($filename) . "\n";
}
?>
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 »

Check the manual: glob()

With what I've told you and the info in the manual - you've got everything you need. Just sit down and plug through it - you should be able to figure it out ;)
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 »

Ah, cool. I have now set up the glob and it is displaying the size and the last modified date for each file. What do i do next?

Sorry about this... i need things spelling out for me. :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

stop reposting for one.
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 »

If you've put the filename and modification date in an array, call asort() on it. Check the manual as to why you want to use it.

The best way to figure stuff out is to try it first. If it doesn't work, try something else. If nothings working, then ask for help.
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 »

All i can think of is to this:

Code: Select all

asort($file);
   while (list ($file, ". date ("F d Y H:i:s.", filemtime($file)) = each ($filelist)) {

So it would turn out as:

Code: Select all

<?php
$dir = "./";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
       }
       closedir($dh);
asort($file);
   while (list ($file, ". date ("F d Y H:i:s.", filemtime($file)) = each ($filelist)) {
   }
}
?>
But then i get this error:

Code: Select all

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ')' in /host/localhost/htdocs/dir/index.php on line 12
:( Spent 3 days trying to do this, havent got anywhere.
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 »

Don't use opendir(), readdir(), and closedir() - glob() is much easier. Read my earlier post - I pretty much gave you a step by step list of what to do and in what order.

If what you've got isn't working, start over.
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 a quick checkup, this is what i have got so far

Code: Select all

<table width='100%' border=0 cellspacing=1 cellpadding=0>
<?php
$glob="*";
chdir(dirname($glob));

foreach (glob(basename($glob)) as $filename) {
   $filepath = dirname($glob)."/".$filename; 
   echo "<tr style='border:1px solid #cfcfcf;background:#fcfcfc; cursor:hand\' onMouseOver=\"this.style.background='#EEEEEE'\" onMouseOut=\"this.style.background='#FFFFFF'\" onClick=\"window.location.href='$file'\">
    <td><a href=\"$filepath\">$filepath</a>
	</td>
    <td align=right><i>" . date ("F d Y H:i:s.", filemtime($filename));
}
?>
</table>
Am i on the right track? And can i now move on to calling the asort() function?
Post Reply