2008年12月19日星期五

Re: [fw-mvc] Deleting files older than 30 days -- qiestion

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJTJtC7bkAtAithusRAiNyAJ9cyld+3yu9+ukjDSBlY+BCNVWm4QCg0qTW
h1Fmg+1LkXhjdDUgnUFjz68=
=NKfK
-----END PGP SIGNATURE-----
I think filemtime() is probably the best timestamp you will get, there
is no "created" time (as mentioned on the filectime() page), but
modified is probably correct (assuming the file is only written once).

So something like:

Raavi Raaj wrote:
> Hi,
>
> I am using thecode below to delete files from the image cache folder.
> How can I modify it to only delete files older than 30 days?
>
> <?php
>
> set_time_limit(900);
>
> delete_folder(dirname(__FILE__) .'/cache/');
> echo '<strong>Done</strong';
>
> function delete_folder($folder) {
> $folder_contents = get_folder_contents($folder);
>
> if ($folder_contents) {
> foreach ($folder_contents as $__content) {
> echo $__content['item'] .'<br />';
> if (is_dir($__content['item']))
> delete_folder($__content['item']);
> else
{
if (time() - filemtime($__content['item']) > (60*60*24*30)){ //
30 days
> unlink($__content['item']);
}
}
> }
> }
> rmdir($folder);
> }
>
> function get_folder_contents($folder) {
> if( !is_dir($folder) ) {
> return false;
> }
> $return_array = array();
> $count = 0;
> if( $dh = opendir($folder) ) {
> while( ($file = readdir($dh)) !== false ) {
> if( $file == '.' || $file == '..' ) continue;
> $return_array[$count]['item'] = $folder .$file .(is_dir($folder
> .$file) ? DIRECTORY_SEPARATOR : '');
>
> $count++;
> }
> closedir($dh);
> }
> return $return_array;
> }
>
> Any suggestions on improving the above code is appreciated.
>
> -R
>

没有评论: