Automatically delete all files after x-time

How do you automatically delete all files in a subdirectory after x-time (say, after 24 hours) - without using the cronjob command from the server or pl. How can you do this simply by using PHP code or simply by visiting a page without clicking something, and the command will start automatically.

+6
file php delete-file
source share
7 answers

The answer to the last comment from my first answer. I'm going to write sample code, so I created a different answer instead of adding another comment.

To delete files with a custom extension, you need to implement the 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 the regular expression /\.txt$/i , which means that only files with the txt extension will be deleted. The '$' sign means that the file name must end with the string '.txt'. The "i" flag indicates that the comparison will be case insensitive. More on the preg_match () function .

Alternatively, you can use strripos () to search for files with a specific extension. Here is the 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. The result of strripos() can also be achieved by combining two functions: strrpos(strtolower($file), '.txt') , but IMHO, this is a good rule to use fewer functions in your code to make it more readable and smaller. Read the warning carefully on the strripos () function page (return value block).

Another important note: if you use a UNIX system, file deletion may fail due to file permissions. You can check the manual for the chmod () function.

Good luck.

+11
source share

You can use the main functions of PHP filectime () and unlink () to check the file creation time 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); } } } 
+7
source share

Ok, here we go, a PHP script that deletes files that are X number of days.

 <? $days = 1; $dir = dirname ( __FILE__ ); $nofiles = 0; if ($handle = opendir($dir)) { while (( $file = readdir($handle)) !== false ) { if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) { continue; } if ((time() - filemtime($dir.'/'.$file)) > ($days *86400)) { $nofiles++; unlink($dir.'/'.$file); } } closedir($handle); echo "Total files deleted: $nofiles \n"; } ?> 

Now paste this code and save it as a php file, upload it to the folder where you want to delete the files. You can see at the beginning of this php code

 $days = 1; 

which sets the number of days, for example, if you set it to 2, files that are older than 2 days will be deleted. Basically this is what happens when the script starts, gets the current directory and reads the file entries, skips'. for the current directory and further checks if there are other directories,

 if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) { continue; } 

if the file entry is not a directory, then it retrieves the modified file time (last modified time) and compares if this is the number of days

 if ((time() - filemtime($dir.'/'.$file)) > ($days *86400)) { $nofiles++; unlink($dir.'/'.$file); } 

if the condition becomes true, it deletes the file using the unlink () php function. Finally closes the directory and exits. I also added a counter to count the number of files to be deleted that will be displayed at the end of the deletion process. So put the php file in the directory that needs to delete the file and execute it.

Hope this helps :)

+2
source share

I use the shell command AT, it looks like a cronjob, though

PHP:

 exec("echo rm /somedir/somefile.ext|at now +24 hours"); 
0
source share

Here is another example that uses GLOB and will delete any file

 $files = glob('path/to/your/files/*'); foreach($files as $file) { // iterate files // if file creation time is more than 5 minutes if ((time() - filectime($file)) > 3600) { // 86400 = 60*60*24 unlink($file); } } 

or if you want to exclude certain files

 $files = preg_grep('#\.txt#', glob('/path/to/your/files/*'), PREG_GREP_INVERT); 
0
source share
 function deleteCachedData($hours=24) { $files = glob(ROOTDIR.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'*.txt'); foreach($files as $file) { if(is_file($file) && (time() - filectime($file)) > $hours*3600) { unlink($file); } } 

}

0
source share

After trying to use these examples, I got into a couple of problems:

  • The comparison operator must be greater than not less than
  • filemtime returns the modified time. filectime is the time when inode file data changes; that is, when permissions, the owner, group, or other metadata from the inode are updated, which may lead to unexpected results.

I changed the example using filemtime and fixed the comparison as follows:

 <?php $path = dirname(__FILE__).'/files'; if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if ((time()-filemtime($path.'/'.$file)) > 86400) { // 86400 = 60*60*24 if (preg_match('/\.txt$/i', $file)) { unlink($path.'/'.$file); } } } } ?> 
0
source share

All Articles