Page 1 of 3
Arranging output of opendir function by last modification?
Posted: Tue Apr 11, 2006 4:48 pm
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!

Posted: Tue Apr 11, 2006 4:52 pm
by feyd
store them into an array with the file modification time. Sort the data.
Posted: Tue Apr 11, 2006 5:05 pm
by ozzy
Thanks, but im not to sure how i would put that together; im a newbie.

Posted: Tue Apr 11, 2006 5:11 pm
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.
Posted: Tue Apr 11, 2006 5:20 pm
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
Posted: Tue Apr 11, 2006 5:29 pm
by feyd
play with arrays for a while, you're figure it out.
Posted: Tue Apr 11, 2006 5:33 pm
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
Posted: Tue Apr 11, 2006 5:44 pm
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";
}
?>
Posted: Tue Apr 11, 2006 5:51 pm
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

Posted: Wed Apr 12, 2006 12:07 pm
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.

Posted: Wed Apr 12, 2006 12:29 pm
by feyd
stop reposting for one.
Posted: Wed Apr 12, 2006 12:54 pm
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.
Posted: Wed Apr 12, 2006 1:17 pm
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.
Posted: Wed Apr 12, 2006 1:50 pm
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.
Posted: Wed Apr 12, 2006 2:22 pm
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?