php - Delete old files - an hour ago or more than an hour ago -
possible duplicate:
how can delete files older 1 hour?
the code down below delete files inside folder called " images ". no complains works should, there way only delete file created hour ago or more hour ago instead? please show me how, i'm trying learn doing, please. try re-use same code down below better understanding , users php programmer around world
<?php define('path', 'images/'); function destroy($dir) { $mydir = opendir($dir); while(false !== ($file = readdir($mydir))) { if($file != "." && $file != "..") { chmod($dir.$file, 0777); if(is_dir($dir.$file)) { chdir('.'); destroy($dir.$file.'/'); rmdir($dir.$file) or die("couldn't delete $dir$file<br />"); } else unlink($dir.$file) or die("couldn't delete $dir$file<br />"); } } closedir($mydir); } destroy(path); echo 'all done.';
you can use filemtime()
or filectime()
. in addition, can't use rmdir()
, because want delete specific files.
what have do
date("u",filectime($file)
- return last time file modified in unix epoch time.unlink($file)
- deletes specified file. have use instead ofrmdir()
.-
an
if
,while
/for
statement needed:while($file) { if(date("u",filectime($file) <= time() - 3600) { unlink($file) } }
then add script:
function destroy($dir) { $mydir = opendir($dir); while($file = readdir($mydir)) { if($file != "." && $file != "..") { chmod($dir.$file, 0777); if(is_dir($dir.$file)) { chdir('.'); while($dir.$file) { if(date("u",filectime($file) >= time() - 3600) { unlink($dir.$file) } } } else unlink($dir.$file) or die("couldn't delete $dir$file<br />"); } } closedir($mydir); }
i hope helps you.
remember:
- there no way file creation time in unix/php
- the current
if
statement returnstrue
if file last changed more or equal 1 hour ago
Comments
Post a Comment