Home  >  Article  >  Backend Development  >  How to delete expired files in php

How to delete expired files in php

王林
王林Original
2021-10-14 17:58:241487browse

php method to delete expired files: [public function download_project($array)$tmp = 'tmp_down';$savepath1 = '../public'.$tmp;$path1 = dir(...] .

How to delete expired files in php

The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.

You must have encountered such a situation, server The disk space occupied is 100%. The reason may be that the temporary folder for project package download is full.

So how should we solve this problem? In fact, it is very simple, just

Solution: Clear the expired files before each download so that they will not take up disk space.

Specific code:

public function download_project($array)
{
// 循环删除过期文件 start
$tmp = 'tmp_down';
$savepath1 = '../public'.$tmp;
$path1 = dir($savepath1);
while (($item = $path1->read())!=false) {
if($item=='.' || $item=='..'){
continue;
}else{
$file = $savepath1.'/'.$item;
$times = time()-filemtime($file);
if($times>24*3600){
unlink($file);
}
}
}

Recommended learning: php training

The above is the detailed content of How to delete expired files in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn