thinkphp Clear Cache/Delete all files on folder

Source: Internet
Author: User
Tags delete cache
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



  • 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.