views:

256

answers:

2

Hi, all, i comeback again with hug questions... maybe someone here can help me...

how to auto delete all files under a sub directory after x-time (let say after 24 hours) but that i want without using cronjob command from server or pl just using php code or just visit the page without clicking something and the command auto run, where i can found this script? regards..

+2  A: 

You can use PHP core functions filectime() and unlink() to check time of file creation and delete its file/files.

EDIT. Code example:

  if ($handle = opendir('/path/to/files')) {

    while (false !== ($file = readdir($handle))) {
        if (filectime($file)< (time()-86400)) {  // 86400 = 60*60*24
          unlink($file);
        }
    }
  }
Alex
It is only skeleton of the function. You can add any conditions, e.g. files with custom extensions or files, that match certain pattern etc.
Alex
thank you alex.... i hope this work..
jones
You're welcome. I'm glad to help you. If this answer is appropriate for your, you can accept it by clicking a tick near my answer.
Alex
i was try add for custom extention file conditions but not work, do u have some examples?
jones
+3  A: 

Response for last comment from my first answer. I'm going to write code sample, so I've created another answer instead of addition one more comment.

To remove files with custom extension you have to implement code:

<?php
  $path = dirname(__FILE__).'/files';
  if ($handle = opendir($path)) {

    while (false !== ($file = readdir($handle))) {
        if ((time()-filectime($path.'/'.$file)) < 86400) {  // 86400 = 60*60*24
          if (preg_match('/\.txt$/i', $file)) {
            unlink($path.'/'.$file);
          }
        }
    }
  }
?>

Comment: 1. This example uses regular expression /\.txt$/i, which means, that only files with extension txt will be removed. '$' sign means, that filename has to be ended with string '.txt'. Flag 'i' indicates, that comparison will be case-insensitive. More about preg_match() function.

Besides you can use strripos() function to search files with certain extension. Here is code snippet:

<?php
  $path = dirname(__FILE__).'/files';
  if ($handle = opendir($path)) {

    while (false !== ($file = readdir($handle))) {
        if ((time()-filectime($path.'/'.$file)) < 86400) {  // 86400 = 60*60*24
          if (strripos($file, '.txt') !== false) {
            unlink($path.'/'.$file);
          }
        }
    }
  }
?>

Comment: This example seems more obvious. Result of strripos() also can be achieved with a combining of two functions: strrpos(strtolower($file), '.txt'), but, IMHO, it's a good rule to use less functions in your code to make it more readable and smaller. Please, read attentively warning on the page of strripos() function(return values block).

One more important notice: if you're using UNIX system, file removing could fail because of file permissions. You can check manual about chmod() function.

Good luck.

Alex
Instead of readdir+preg_match why not simply use glob()?
Jasper Bekkers
You're right. I didn't write about glob() function simply because I have never use it before.:) Thank you for your comment.
Alex
Noted Alex and Thankyou so much
jones