Php regularly deletes files in a folder (clears cached files ). 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: If this code is always executed without restarting the website, is there any way to automatically clear the temporary folder?
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.
The code is as follows:
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.
The code is as follows:
$ 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.
The 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 ).
The code is as follows:
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 );
?>
Why? The following code is a php code that cleans up files in a folder at regular intervals. Ps: This code will always be executed if the website is not restarted...