php 定时删除文件实现方法

内容摘要
这篇文章主要为大家详细介绍了php 定时删除文件实现方法,具有一定的参考价值,可以用来参考一下。

这段代码可以在指定的时间过后删除指定的文件,用这段代码来清除缓存文件和临
文章正文

这篇文章主要为大家详细介绍了php 定时删除文件实现方法,具有一定的参考价值,可以用来参考一下。

这段代码可以在指定的时间过后删除指定的文件,用这段代码来清除缓存文件和临时文件再好不过了。对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<code class="php">
/**
 * 在指定的时间过后删除指定的文件
 *
 * @param
 * @arrange (www.idcnote.com)
 **/
// Define the folder to clean
// (keep trailing slashes)
$captchaFolder  = 'temp/';
  
// Filetypes to check (you can also use *.*)
$fileTypes      = '*.jpg';
  
// Here you can define after how many
// minutes the files should get deleted
$expire_time    = 20;
  
// Find all files of the given file type
foreach (glob($captchaFolder . $fileTypes) as $Filename) {
  
    // Read file creation time
    $FileCreationTime = filectime($Filename);
  
    // Calculate file age in seconds
    $FileAge = time() - $FileCreationTime;
  
    // Is the file older than the given time span?
    if ($FileAge > ($expire_time * 60)){
  
        // Now do something with the olders files...
  
        print "The file $Filename is older than $expire_time minutes\n";
  
        // For example deleting files:
        //unlink($Filename);
    }
  
}
 
 
/***   来自php教程(www.idcnote.com)   ***/</code>

注:关于php 定时删除文件实现方法的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!