...

View Full Version : PHP Code Explanation



trancedm
10-26-2006, 02:59 AM
Hey guys,

I have the code below and I need some help with it. I did not wrote this code so im not too sure how to edit it...



$var = (time() - filemtime("dir/$file"))/60/60/240;
if ($var > 1) unlink("dir/$file");


Basically the code above deletes a file when it reaches a certain amount of days but I can't figure out when exactly it deletes the file. I want to know so I can either increase/decrease the amount of days when the file will be deleted, im assuming it has something to do with the "filemtime" function but I can't undertand the mathematics to it so if anyone know and willing to explain it to me that would be great. :)

Any help will be greatly appreciated.

Thanks

Fumigator
10-26-2006, 08:18 AM
It's using the unix timestamp, which is the number of seconds since January 1 1970 00:00:00 GMT. The time() function returns "right now", and the filemtime() function returns the timestamp on the file. That code is taking the difference between those two timestamps and dividing it by 60, giving the number of minutes between the two timestamps, then 60 again, giving the number of hours between the two timestamps, then by 240, giving "how many 240 hours" are between the two timestamps. If there are more than 240 hours between the two timestamps, then the value comes back greater than 1.

So... basically... a 10 day old file (or greater) gets deleted.

GJay
10-26-2006, 08:24 AM
close, but not quite.
filemtime() returns the time of the last modification to the file, not the creation date.

Fumigator
10-26-2006, 08:30 AM
Aha, but I was intentionally obtuse on that point-- I just said the "timestamp on the file" :D

trancedm
10-26-2006, 09:03 AM
It's using the unix timestamp, which is the number of seconds since January 1 1970 00:00:00 GMT. The time() function returns "right now", and the filemtime() function returns the timestamp on the file. That code is taking the difference between those two timestamps and dividing it by 60, giving the number of minutes between the two timestamps, then 60 again, giving the number of hours between the two timestamps, then by 240, giving "how many 240 hours" are between the two timestamps. If there are more than 240 hours between the two timestamps, then the value comes back greater than 1.

So... basically... a 10 day old file (or greater) gets deleted.

Thanks for the explantion, I sort of get it.. so if I want to delete the file in let say 5 days, what do I need to change, care to explain? :)

Green rep added btw.

NancyJ
10-26-2006, 09:04 AM
Thanks for the explantion, I sort of get it.. so if I want to delete the file in let say 5 days, what do I need to change, care to explain? :)

Green rep added btw.

Change the 240 to 120 - ie the number of hours in 5 days

trancedm
10-26-2006, 09:08 AM
Aha! Thanks NancyJ!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum