filemtime() of a Directory???

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

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

filemtime() of a Directory???

Post by Chris Corbyn »

Hi,

Is there a function which gets the last modifed time of a directory or the created time of it?

I know you can do it with files using filemtime() - I've used it many times - but it doesn't seem to work for directories i.e.

Code: Select all

$modified = filemtime('C:\\Directory\\Subdirectory');
I've basically got a list of directories (in an array) which I want PHP to sort by the last modified/created time.

Any ideas?

PS: I've tried filectime too but I get the same (apparently) random ordering of directories. I know what the newest one is and it never even appears in the top 10.

Thanks in advance :-)
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Ummm you tried stat() ?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I'm looking at it now but if I'm using it correctly (documentation isn't too good for it) then I think it doesn't work neither.

I'm doing this:

Code: Select all

$dir = 'C:\\My Albums\''.$subdir;
$stats_array = stat($dir);

$mod_time = array_slice($stats_array, 9, 1);
I'll test it by making a folder right now and converting what stat brings back as it's unix timestamp to one I can read and see if it's correct. Don't think it works though.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Tried this to test it...

Code: Select all

<?php

$stat_array = stat('C:\\testfolder');

$modtime = array_slice($stat_array, 9, 1);

echo $modtime;

?>
... and the output is
Array

I must be using the function wrong.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I'm such a tool.... What was I thinking??? I should have echoed $modtime[0] not just the little slice of the array.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Nope, still the same. The order I get my directories listed in never changes but it's wrong...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ok, Now I've started adding new folders to my parent directory it seems to read the newest ones as just that. But all the others seem to be just random. Oh, now I've got an idea what it might be.... I've changed the files within the folders prior to doing this and maybe it's reading that as a direactory modification....

Probably explains it.

Thanks for the tip! :-)
Post Reply