Php regularly deletes files in folders (clears cached files)

Source: Internet
Author: User
Sometimes there are more and more temporary files generated in the cached folder of the website. if the files are not cleaned up for a long time, tens of thousands of cached files will exist in the folder, and many ftp tools may not be able to access the folder. So that temporary files cannot be deleted, 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.
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 );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.