php删除临时文件的代码示例

352 阅读1分钟
我们有时候需要删除刚生成的临时文件,比如上传图片或者生成图片的时候,我们需要现在本地存储起来,然后再上传到图片服务器。当图片上传到服务器之后,那本地存储的图片就没用了,为了避免项目文件过大,所以删除本地的图片文件的就变得很有必要。
直接分享一段代码:
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
//需要传两个参数,一个是我们需要删除的文件路径,比如:
$path2= "./upload/picture/";
$this->delDirAndFile($path,true);

//这是删除的方法
public function delDirAndFile($path, $delDir = true) {
if (is_array($path)) {
foreach ($path as $subPath)
$this->delDirAndFile($subPath, $delDir);
}
if (is_dir($path)) {
$handle = opendir($path);
if ($handle) {
while (false !== ( $item = readdir($handle) )) {
if ($item != "." && $item != "..")
is_dir("$path/$item") ? $this->delDirAndFile("$path/$item", $delDir) : unlink("$path/$item");
}
closedir($handle);
if ($delDir)
return rmdir($path);
}
} else {
if (file_exists($path)) {
return unlink($path);
} else {
return FALSE;
}
}
clearstatcache();
}

以上就删除文件夹或者文件的代码部分。
以上就是php删除临时文件的代码示例的详细内容

更多技术资讯可关注:gzitcast