Home  >  Article  >  Backend Development  >  How to delete files regularly in php?

How to delete files regularly in php?

coldplay.xixi
coldplay.xixiOriginal
2020-07-10 09:35:183133browse

How to delete files regularly in php: first create a new [flag.txt] file and enter 1 or 0 in it; then [1] means to continue execution, the code is [$flag = 1;]; finally the client When the client is disconnected, the script can continue to execute in the background, the code is [ignore_user_abort();].

How to delete files regularly in php?

How to regularly delete files in php:

The following code is a simple php that regularly cleans files in a folder code.

ps: This code will continue to be executed if the website is not restarted, so it is only used for local environment testing. Please do not test on the website.

The code is as follows:

<?php
ignore_user_abort(); //客户端断开时,可以让脚本继续在后台执行
set_time_limit(0); //忽略php.ini设置的脚本运行时间限制
$interval = 5*60; //设置执行周期,单位为秒,5分钟为 5*60=300
do{
$dir = "temp/"; //你的临时目录位置
$handle=opendir("{$dir}/");
while (false !== ($file=readdir($handle))) {
if ($file!="." && $file!=".." && !is_dir("{$dir}/{$file}")) {
@unlink ("{$dir}/{$file}");
}
}
closedir($handle); //关闭由 opendir() 函数打开的目录
sleep($interval); //执行一个周期后,休眠$interval时间,休眠结束后脚本继续执行
}while(true); //周期性执行脚本

Based on an idea found on the Internet, create a flag.txt file and enter 1 or 0 in it. "0" means to stop execution, and "1" means to continue. implement. This way it can be started and stopped.

The code is as follows:

<?php
$flag = 1; //将执行标志设置为1,默认为执行
ignore_user_abort(); //客户端断开时,可以让脚本继续在后台执行
set_time_limit(0); //忽略php.ini设置的脚本运行时间限制
$interval = 5*60; //设置执行周期,单位为秒,5分钟为 5*60=300
do{
$flagfile = "flag.txt"; //标志放置在文件“flag.txt”中。“0”表示停止执行,“1”表示继续执行
if(file_exists($flagfile) && is_readable($flagfile)) { //读取文件内容
$fh = fopen($flagfile,"r");
while (!feof($fh)) {
$flag = fgets($fh); //存储标志
}
fclose($fh);
}
$dir = "temp/"; //你的临时目录位置
$handle=opendir("{$dir}/");
while (false !== ($file=readdir($handle))) {
if ($file!="." && $file!=".." && !is_dir("{$dir}/{$file}")) {
@unlink ("{$dir}/{$file}");
}
}
closedir($handle); //关闭由 opendir() 函数打开的目录
sleep($interval); //执行一个周期后,休眠$interval时间,休眠结束后脚本继续执行
}while($flag);

php deletes the folder and all files under the folder

The code is as follows:

<?
function deldir($dir) {
//先删除目录下的文件:
$dh=opendir($dir);
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
deldir($fullpath);
}
}
}
closedir($dh);
//删除当前文件夹:
if(rmdir($dir)) {
return true;
} else {
return false;
}
}
?>

Example:Delete all ".svn" folders under a certain folder (including their contents must also be deleted).

The code is as follows:

<?php
function delsvn($dir) {
$dh=opendir($dir);
//找出所有".svn“ 的文件夹:
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(is_dir($fullpath)) {
if($file==".svn"){
delsvndir($fullpath);
}else{
delsvn($fullpath);
}
}
}
}
closedir($dh);
}
function delsvndir($svndir){
//先删除目录下的文件:
$dh=opendir($svndir);
while($file=readdir($dh)){
if($file!="."&&$file!=".."){
$fullpath=$svndir."/".$file;
if(is_dir($fullpath)){
delsvndir($fullpath);
}else{
unlink($fullpath);
}
}
}
closedir($dh);
//删除目录文件夹
if(rmdir($svndir)){
return true;
}else{
return false;
}
}
$dir=dirname(__FILE__);
//echo $dir;
delsvn($dir);
?>

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of How to delete files regularly 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

Related articles

See more