php如何删除临时文件
php删除临时文件的方法:首先打开相应的PHP文件;然后在文件中传入两个参数,其中一个是需要删除的文件路径;接着创建一个delDirAndFile方法用于实现删除功能;最后保存文件即可。
直接分享一段代码://需要传两个参数,一个是我们需要删除的文件路径,比如:
$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如何删除临时文件相关教程