thinkphp Clear Cache/Delete all files under folder
Today did a thinkphp to clear the cache function, on the internet opened a little bit of a simpler way of writing but that is a thinkphp built-in class, I found a half-day did not find this class, gas dead I have to use PHP to delete all files under the file this method to achieve the removal of the slowly saved function, Don't talk much about sticking the code:
/* This method is used by public methods to delete all files under a folder
* $path path to the file
* $fileName folder name
* */
Public Function Rmfile ($path, $fileName) {
Remove spaces
$path = Preg_replace ('/(\/) {2,}|{ \\\}{1,}/', '/', $path);
Get the full Catalog
$path. = $fileName;
Determine if this file is a file directory
if (Is_dir ($path)) {
Open File
if ($dh = Opendir ($path)) {
Traverse file Directory Name
while (($file = Readdir ($DH)) = = False) {
Delete them individually
Unlink ($path. ' \ \ '. $file);
}
Close File
Closedir ($DH);
}
}
}
One-to-one delete cache
Public Function cache () {
The front desk uses Ajax get to commit, here is to judge first
if ($_get[' type ']) {
Gets the value submitted (that is, the name of the folder to be deleted).
$type =$_get[' type '];
Gets the absolute path of the file
$abs _dir=dirname (DirName (DirName (dirname (__file__)));
Combine files in the folder you want to delete
$pa = $abs _dir. ' \admin\runtime\\ ';
Call the method written above
$this->rmfile ($PA, $type);
Back to prompt information
$this->ajaxreturn (1, ' clear Success ', 1);
}else{
$this->display ();
}
}
Clear all caches with one click
Public Function Allrun () {
The front desk uses Ajax get to commit, here is to judge first
if ($_get[' type ']) {
Get the value passed over
$type =$_get[' type '];
Will pass over the value to cut, I have "-" to cut the
$name =explode ('-', $type);
The number of strips to be cut to facilitate the loop below
$count =count ($name);
Loop calls the above method
for ($i =0; $i < $count; $i + +) {
Get the absolute path to the file
$abs _dir=dirname (DirName (DirName (dirname (__file__)));
Combining paths
$pa = $abs _dir. ' \admin\runtime\\ ';
method to invoke all files under Delete folder
$this->rmfile ($pa, $name [$i]);
}
Give a hint
$this->ajaxreturn (1, ' clear Success ', 1);
}else{
$this->display ();
}
}
The front page is simple enough to give important code here.
HTML file
This is the Ajax get way to commit
One-click Clears all