Is there any way to automatically clear temporary folders?
The following code is a php code that cleans up files in a folder at regular intervals.
Ps: this code will continue to be executed without restarting the website, so it is only used for local environment testing. Please do not test it on the website.
Copy codeThe Code is as follows:
<? Php
Ignore_user_abort (); // when the client is disconnected, the script can be executed in the background.
Set_time_limit (0); // ignore the script running time limit set by php. ini
$ Interval = 5*60; // set the execution cycle, in seconds, 5 minutes to 5*60 = 300
Do {
$ Dir = "temp/"; // your temporary 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 a cycle is executed, sleep $ interval takes effect. After sleep ends, the script continues to run.
} While (true); // periodically execute the script
If you create a flag.txt file and enter 1 or 0 in the file, "0" indicates that execution is stopped, and "1" indicates that execution continues. In this way, you can start and stop.
Copy codeThe Code is as follows:
<? Php
$ Flag = 1; // set the execution flag to 1. The default value is execution.
Ignore_user_abort (); // when the client is disconnected, the script can be executed in the background.
Set_time_limit (0); // ignore the script running time limit set by php. ini
$ Interval = 5*60; // set the execution cycle, in seconds, 5 minutes to 5*60 = 300
Do {
$ Flagfile = "flag.txt"; // The flag is placed in the “flag.txt file. "0" indicates that execution is stopped, and "1" indicates that execution is continued.
If (file_exists ($ flagfile) & is_readable ($ flagfile) {// Read File Content
$ Fh = fopen ($ flagfile, "r ");
While (! Feof ($ fh )){
$ Flag = fgets ($ fh); // store the flag
}
Fclose ($ fh );
}
$ Dir = "temp/"; // your temporary 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 a cycle is executed, sleep $ interval takes effect. After sleep ends, the script continues to run.
} While ($ flag );
Php deletes all files in folders and their folders.
Copy codeThe Code is 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 the current folder:
If (rmdir ($ dir )){
Return true;
} Else {
Return false;
}
}
?>
Instance: delete all the ". svn" folders under a folder (including the content to be deleted ).
Copy codeThe Code is 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 the files in the 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 the directory folder
If (rmdir ($ svndir )){
Return true;
} Else {
Return false;
}
}
$ Dir = dirname (_ FILE __);
// Echo $ dir;
Delsvn ($ dir );
?>