Page 1 of 1

php to check age of a file

Posted: Mon Nov 06, 2006 6:43 am
by RICKYJ
does anyone know how i can use php to check the age of a file

I have simple bit of code that downloads rss feed to my local host, but I only want it to run if
1. the downloaded file doesnt exist
2. the downloaded file is more than say 2 hours old

Posted: Mon Nov 06, 2006 6:57 am
by jayshields
I don't know of a function to check creation time, but last modification time can be accessed using

Code: Select all

filemtime()
, if a file exists use

Code: Select all

file_exists()

Posted: Mon Nov 06, 2006 7:21 am
by amir
What operating system you are using?

If Linux / Unix, then
information can not be get. The kernel only stores three different times:
atime
Time of last access as seconds since Epoch

mtime
Time of last modification as seconds since Epoch

ctime
Time of last change as seconds since Epoch

man stat or see /usr/include/linux/stat.h

Posted: Mon Nov 06, 2006 8:24 am
by RICKYJ
Thanks JAY that helped :D
possibly unix, but mtime seemed to work for me

I used :

Code: Select all

if ( (date('h') - date('h', filemtime("TempRSSFeed.txt")) ) > x) { // then do code
all seems to work, obviously this wont work very well if ppl only use it once every 24hrs (difference will be 0), but expect the page to be looked at at least once every few hours.

Is there a way around since i want to release this as free ware (im new to php), eg convert date to absolute seconds