Is there a way to automatically clean up temporary folders?
The following code is a simple php code for periodically cleaning files in a folder. ps: this code will run until the site is rebooted, so it should only be used to test the local environment. Please do not test on the site.
<?php
ignore_user_abort(); // When the client is disconnected, you can let the script continue executing in the background
set_time_limit(0); // ignore php.ini Set the script run time limit
$interval = 5*60; // Set the execution cycle in seconds, 5 Minutes for 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 by opendir() The function opens the directory
sleep($interval); // perform 1 After 4 cycles, hibernate $interval Time, the script continues execution after hibernation
}while(true); // Periodically execute scripts
According to an idea on the Internet, build an flag.txt file and enter 1 or 0 in it. “0” means stop execution, and “1” means continue execution. That way you can start and stop.
<?php
$flag = 1; // Set the execution flag to 1 , is executed by default
ignore_user_abort(); // When the client is disconnected, you can let the script continue executing in the background
set_time_limit(0); // ignore php.ini Set the script run time limit
$interval = 5*60; // Set the execution cycle in seconds, 5 Minutes for 5*60=300
do{
$flagfile = "flag.txt"; // Logo placed in file" flag.txt ". " 0 "Stands for stop execution," 1 "Means to continue the execution
if(file_exists($flagfile) && is_readable($flagfile)) { // Read file contents
$fh = fopen($flagfile,"r");
while (!feof($fh)) {
$flag = fgets($fh); // Store logo
}
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 by opendir() The function opens the directory
sleep($interval); // perform 1 After 4 cycles, hibernate $interval Time, the script continues execution after hibernation
}while($flag);
php deletes folders and all files under them
<?
function deldir($dir) {
// First, delete the files in the directory:
$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;
}
}
?>
Example: delete all “.svn “folders under a folder (including its contents).
<?php
function delsvn($dir) {
$dh=opendir($dir);
// Find out all ".svn " Folder:
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){
// First, delete the files in the directory:
$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 folder
if(rmdir($svndir)){
return true;
}else{
return false;
}
}
$dir=dirname(__FILE__);
//echo $dir;
delsvn($dir);
?>