So is there a way to automatically clean up temporary folders?
The following code is a simple time to clean up the file under the folder PHP code.
PS: This code, if you do not restart the site will continue to perform, so only for the local environment test, please do not test on the site.
Copy Code code as follows:
<?php
Ignore_user_abort (); When the client disconnects, you can have the script continue to execute in the background
Set_time_limit (0); Ignore script run time limits for php.ini settings
$interval = 5*60; Set the execution cycle in seconds, 5 minutes for 5*60=300
do{
$dir = "temp/"; Your Temp directory location
$handle =opendir ("{$dir}/");
while (False!== ($file =readdir ($handle))) {
if ($file!= "." && $file!= "..." &&!is_dir ("{$dir}/{$file}")) {
@unlink ("{$dir}/{$file}");
}
}
Closedir ($handle); Close the directory opened by the Opendir () function
Sleep ($interval); After performing a cycle, hibernate $interval time, scripts continue to execute after hibernation is over
}while (TRUE); Execute scripts periodically
According to a line of thought, build a Flag.txt file, enter 1 or 0 inside, "0" means to stop execution, "1" means to continue execution. That way you can start and stop.
Copy Code code as follows:
<?php
$flag = 1; Set the execution flag to 1, default to execute
Ignore_user_abort (); When the client disconnects, you can have the script continue to execute in the background
Set_time_limit (0); Ignore script run time limits for php.ini settings
$interval = 5*60; Set the execution cycle in seconds, 5 minutes for 5*60=300
do{
$flagfile = "Flag.txt"; The flag is placed in the file "Flag.txt". "0" means stop execution, and "1" indicates continued execution
if (file_exists ($flagfile) && is_readable ($flagfile)) {//Read file contents
$fh = fopen ($flagfile, "R");
while (!feof ($FH)) {
$flag = fgets ($FH); Storage Flags
}
Fclose ($FH);
}
$dir = "temp/"; Your Temp directory location
$handle =opendir ("{$dir}/");
while (False!== ($file =readdir ($handle))) {
if ($file!= "." && $file!= "..." &&!is_dir ("{$dir}/{$file}")) {
@unlink ("{$dir}/{$file}");
}
}
Closedir ($handle); Close the directory opened by the Opendir () function
Sleep ($interval); After performing a cycle, hibernate $interval time, scripts continue to execute after hibernation is over
}while ($flag);
PHP deletes all files under Folders and folders
Copy Code code as follows:
?
function Deldir ($dir) {
Delete the files in the directory first:
$DH =opendir ($dir);
while ($file =readdir ($DH)) {
if ($file!= "." && $file!= "...") {
$fullpath = $dir. " /". $file;
if (!is_dir ($fullpath)) {
Unlink ($fullpath);
} else {
Deldir ($fullpath);
}
}
}
Closedir ($DH);
Delete current folder:
if (RmDir ($dir)) {
return true;
} else {
return false;
}
}
?>
Instance: Deletes all the ". SVN" folders under a folder, including their contents.
Copy Code code as follows:
<?php
function delsvn ($dir) {
$dh =opendir ($dir);
//Find All ". SVN" folders:
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) {
///delete files under directory first:
$dh =opendir ($svndir);
while ($file =readdir ($DH)) {
if ($file!= ".") && $file!= "...") {
$fullpath = $svndir. " /". $file;
if (Is_dir ($fullpath)) {
Delsvndir ($fullpath);
}else{
Unlink ($fullpath);
}
}
}
Closedir ($DH);
//Delete directory folders
if (RmDir ($svndir)) {
return true;
}else{
return false;
}
$dir =dirname (__file__);
//echo $dir;
Delsvn ($dir);
?>